Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tic Tac Toe Kata

Tic Tac Toe Kata

The rules of the tic tac toe game are the following:

  • a game is over when all fields are taken
  • a game is over when all fields in a column are taken by a player
  • a game is over when all fields in a row are taken by a player
  • a game is over when all fields in a diagonal are taken by a player
  • a player can take a field if not already taken
  • players take turns taking fields until the game is over
  • there are two player in the game (X and O)
@hamzatorjmen

This comment has been minimized.

hamzatorjmen commented Sep 3, 2018

`import java.util.Scanner;

public class SG {

static String[] tictactoe;
static boolean winner = false;

public static void main(String[] args) {
	int i = 0;
	tictactoe = new String[9];
	int playerInput;
	String turn = "";
	Scanner in = new Scanner(System.in);
	setTicTacToe();
	displayTicTacToe();
	System.out.println("Turn of X : ");
	while (!winner) {
		System.out.println(turn);
		playerInput = in.nextInt();
		if (playerInput >= 1 && playerInput <= 9) {
			i++;
			if (i % 2 != 0) {
				tictactoe[playerInput - 1] = "X";
				turn = "Turn of Y : ";
			} else if (i % 2 == 0) {
				tictactoe[playerInput - 1] = "O";
				turn = "Turn of X : ";
			}
			displayTicTacToe();
			checkWinner();
		} else {
			System.out.println("wrong number");
		}
	}
}

static void checkWinner() {
	String check = "";
	for (int i = 0; i < 9; i++) {
		check = "";
		switch (i) {
		case 0:
			check = tictactoe[0] + tictactoe[1] + tictactoe[2];
			break;
		case 1:
			check = tictactoe[3] + tictactoe[4] + tictactoe[5];
			break;
		case 2:
			check = tictactoe[6] + tictactoe[7] + tictactoe[8];
			break;
		case 3:
			check = tictactoe[0] + tictactoe[3] + tictactoe[6];
			break;
		case 4:
			check = tictactoe[1] + tictactoe[4] + tictactoe[7];
			break;
		case 5:
			check = tictactoe[2] + tictactoe[5] + tictactoe[8];
			break;
		case 6:
			check = tictactoe[0] + tictactoe[4] + tictactoe[8];
			break;
		case 7:
			check = tictactoe[2] + tictactoe[4] + tictactoe[6];
			break;
		}
		if (check.equals("XXX")) {
			System.out.println("X win the game !!");
			winner = true;
		} else if (check.equals("OOO")) {
			System.out.println("O win the game !!");
			winner = true;
		}
	}
}

static void setTicTacToe() {
	for (int i = 0; i < 9; i++) {
		tictactoe[i] = String.valueOf(i + 1);
	}
}

static void displayTicTacToe() {
	System.out.println("|-- |-- |-- |");
	System.out.println("| " + tictactoe[0] + " | " + tictactoe[1] + " | " + tictactoe[2] + " |");
	System.out.println("|-- |-- |-- |");
	System.out.println("| " + tictactoe[3] + " | " + tictactoe[4] + " | " + tictactoe[5] + " |");
	System.out.println("|-- |-- |-- |");
	System.out.println("| " + tictactoe[6] + " | " + tictactoe[7] + " | " + tictactoe[8] + " |");
	System.out.println("|-- |-- |-- |");
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment