About Me

Game SOS

Dalam postingan saya kali ini, saya akan memberikan source code tentang permainan yang umum dan sederhana yang biasa di temukan di kehidupan sehari-hari, yaitu game SOS/XOX atau sejenisnya, langsung saja berikut saya cantumkan source codenya yg menggunakan program java netbeans...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sos;

import java.util.Scanner;

/**
 *
 * @author Chiput_Konan
 */
public class Sos {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String ar[] = {"-","-","-","-","-","-","-","-","-"};
 int player1 = 0;
 int player2 = 0;
 int index = 0;

 for(int l = 0; l<3; l++)
 {
   for(int j = 0; j<3; j++)
   {
     System.out.print(ar[j]);
   }
   System.out.print("\n");
 }

 for(int j = 0;j < 9;j++)
 {
  //Input position and letter
  System.out.println("Enter the position number: ");
  index = input.nextInt();
  input.nextLine();
  System.out.println("Enter (S or O): ");
  ar[index - 1] = input.nextLine();

  //Output for the game
  for(int l = 0; l<9; l++)
  {
    System.out.print(ar[l]);
    if(l == 2)
    {
      System.out.println();
    }
    else if(l == 5)
    {
      System.out.println();
    }
    else if(l == 8)
    {
      System.out.println();
    }
  }
  //condition
  if((ar[0]+ar[1]+ar[2]).equals("SOS"))
  {
    if(j%2 == 0)
    {
      player1++;
      System.out.println("Player 1: "+player1+"    Player 2: "+player2);
    }
    else if( j % 2 != 0)
    {
      player2++;
      System.out.println("Player 1: "+player1+"    Player 2: "+player2);
    }
  }
  else if((ar[3]+ar[4]+ar[5]).equals("SOS"))
  {
    if(j%2 == 0)
    {
      player1++;
      System.out.println("Player 1: "+player1+"    Player 2: "+player2);
    }
    else if( j % 2 != 0)
    {
      player2++;
      System.out.println("Player 1: "+player1+"    Player 2: "+player2);
    }
  }
  else
  {
    System.out.println("Player 1: "+player1+"    Player 2: "+player2);
  }



  //end of condition

  }
  }
  }