Skip to content

Instantly share code, notes, and snippets.

@jonathan-irvin
Created April 16, 2015 19:43
Show Gist options
  • Save jonathan-irvin/7e21fb37f208ae6746cb to your computer and use it in GitHub Desktop.
Save jonathan-irvin/7e21fb37f208ae6746cb to your computer and use it in GitHub Desktop.
TicTacToe
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[][] grid = new char[3][3];
//initialize array
for (int row = 0; row < grid.length; row++){
for (int col = 0; col < grid[0].length; col++){
grid[row][col] = ' ';
}
}
int turn = 1;
char player = 'X';
boolean winner = false;
//play a turn
while (winner == false && turn <= 9){
boolean validPlay;
int column;
int rowe;
do {
display(grid);
System.out.print("Player " + player + ", choose a column: ");
column = in.nextInt();
System.out.print("Player " + player + ", choose a row: ");
rowe = in.nextInt();
//validate play
validPlay = validate(column, rowe, grid);
}while (validPlay == false);
//drop the checker
for (int row = 0; row <= grid.length; row++){
if(grid[rowe][column] == ' '){
grid[rowe][column] = player;
break;
}
}
//determine if there is a winner
winner = isWinner(player,grid);
//switch players
if (player == 'X'){
player = 'O';
}else{
player = 'X';
}
turn++;
} //end of turn
display(grid);
if (winner == true) {
if (player == 'X') {
System.out.println("O's won!");
}
else {
System.out.println("X's won!");
}
}
else {
System.out.println("Tie game");
}
}
public static void display(char[][] grid){
System.out.println(" 0 1 2");
for (int row = 0; row < grid.length; row++){
if (row == 0)
System.out.print("0 ");
if (row == 1)
System.out.print("1 ");
if (row == 2)
System.out.print("2 ");
for (int col = 0; col < grid[0].length; col++){
System.out.print(grid[row][col]);
if (col < 2) {
System.out.print("|");
}
}
System.out.println();
if (row < 2) {
System.out.println(" -+-+-");
}
}
System.out.println();
}
public static boolean validate(int column, int rowe, char[][] grid){
//valid column?
if (column < 0 || column > grid[0].length){
return false;
}
//valid row?
if (rowe < 0 || rowe > grid[0].length){
return false;
}
//full column / row?
if (grid[rowe][column] != ' '){
return false;
}
return true;
}
public static boolean isWinner(char player, char[][] grid){
//check for 3 across
for(int row = 0; row<grid.length; row++){
for (int col = 0;col < grid[0].length - 2;col++){
if (grid[row][col] == player &&
grid[row][col+1] == player &&
grid[row][col+2] == player){
return true;
}
}
}
//check for 3 up and down
for (int row = 0; row < grid.length - 2; row++) {
for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == player &&
grid[row+1][col] == player &&
grid[row+2][col] == player) {
return true;
}
}
}
//check diagonal
for (int row = 2; row < grid.length; row++) {
for (int col = 0; col < grid[0].length - 2; col++) {
if (grid[row][col] == player &&
grid[row - 1][col+1] == player &&
grid[row - 2][col+2] == player) {
return true;
}
}
}
for (int row = 0; row < grid.length - 2; row++) {
for (int col = 0; col < grid[0].length - 2; col++) {
if (grid[row][col] == player &&
grid[row + 1][col+1] == player &&
grid[row + 2][col+2] == player) {
return true;
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment