Skip to content

Instantly share code, notes, and snippets.

@eroltutumlu
Created February 28, 2015 11:10
Show Gist options
  • Save eroltutumlu/a81232e8736d9d3f0655 to your computer and use it in GitHub Desktop.
Save eroltutumlu/a81232e8736d9d3f0655 to your computer and use it in GitHub Desktop.
Simple TicTacToe Game | JAVA
package JavaOgreniyorum;
// Main class'ından obje oluşturulup çağırılmalıdır.
import java.util.Scanner;
public class XOXGame {
private final int cSIZE = 3;
private final int rSIZE = 3;
private char chrXO = 'X';
char[][] gameBoard = new char[rSIZE][cSIZE];
int[] gameIndex = new int[9];
public void GameBoard(){
for(int i = 0; i < rSIZE; ++i)
{
for(int j = 0; j < cSIZE; ++j)
{
gameBoard[i][j] = '-';
}
}
PlayGame();
}
public void printGameBoard(){
for(int i = 0; i < rSIZE; ++i)
{
for(int j = 0; j < cSIZE; ++j)
{
System.out.print(gameBoard[i][j]+" ");
}
System.out.println();
}
}
public void PlayGame()
{
printGameBoard();
boolean playGame = true;
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
while(playGame){
System.out.println("Satır sayısını giriniz: ");
int row = scan.nextInt() - 1;
System.out.println("Sütun sayısını giriniz: ");
int col = scan.nextInt() - 1;
try{
if(gameBoard[row][col]!='-'){
System.out.println("Geçersiz bir işlem yaptınız.");
}
else
{
gameBoard[row][col] = chrXO;
printGameBoard();
if(isXOX(row,col)){
System.out.println("Tebrikler");
playGame = false;
}
if(chrXO == 'X' && gameBoard[row][col] == 'X')
chrXO = 'O';
if(chrXO == 'O' && gameBoard[row][col] == 'O')
chrXO = 'X';
}
}
catch(IndexOutOfBoundsException e){
System.out.println("Geçersiz işlem");
}
}
}
public boolean isXOX(int row, int col)
{
if(gameBoard[0][col]==gameBoard[1][col] && gameBoard[0][col]==gameBoard[2][col]){
return true;
}
else if(gameBoard[row][0]==gameBoard[row][1] && gameBoard[row][0]==gameBoard[row][2]){
return true;
}
else if(gameBoard[0][0]==gameBoard[1][1] && gameBoard[0][0]==gameBoard[2][2] && gameBoard[0][0]!='-'){
return true;
}
else if(gameBoard[0][2]==gameBoard[1][1] && gameBoard[0][2]==gameBoard[2][0] && gameBoard[0][2]!='-'){
return true;
}
else
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment