Using java, I create a program where the user can play tic tac toe against the computer. I used a 2d array to create the game board and then I create a for loop to display the board. Switch statements and loops were used for checking that the computer and the user didn't move into the same spots. The java scanner library was used to get input from the user and then would ask if the user to make a move between 1-9, which corresponded to spaces on the gameboard. I the user won, a message would be displayed that said "you have won", but if the computer won, a message would be displayed that said you lost.
import java.util.*;
public class tictactoe {
public static ArrayList<Integer>playerPositions = new ArrayList<Integer>();
public static ArrayList<Integer>computerPositions = new ArrayList<Integer>();
public static void main(String[] args) {
char[][] board ={{' ', '|', ' ', '|', ' '},
{'-', '+', ' ', '+', '-'},
{' ', '|',' ', '|', ' '},
{'-', '+',' ', '+', '-'},
{' ', '|',' ', '|', ' '}};
while (true){
Scanner scan = new Scanner(System.in);
System.out.println("Make your move: (1-9):");
int pos =scan.nextInt();
while(playerPositions.contains(pos)|| computerPositions.contains(playerPositions)){
System.out.println("Position taken! Enter a correct position!");
pos = scan.nextInt();
}
place(board,pos, "player" );
String result = Winner();
if(result.length() > 0){
System.out.println(result);
break;
}
Random rand = new Random();
int computerPos = rand.nextInt(9) +1;
while(playerPositions.contains(computerPos)|| computerPositions.contains(computerPos)){
computerPos = rand.nextInt(9) +1;
}
place(board, computerPos, "computer");
drawBoard(board);
}
}
//draws the game board
public static void drawBoard(char[][] board){
for (int i =0; i <5;i++){
for (int j =0; j <5; j++){
System.out.print(board[i][j]);
}
System.out.println();
}
}
// places the pieces on the board
public static void place(char[][] board, int pos, String user ){
char symbol = ' ';
if(user.equals("player")){
symbol = 'X';
playerPositions.add(pos);
}else if(user.equals("computer")){
symbol = 'O';
computerPositions.add(pos);
}
//row column
switch(pos){
case 1:
board[0][0] = symbol;
break;
case 2:
board[0][2] = symbol;
break;
case 3:
board[0][4] = symbol;
break;
case 4:
board[2][0] = symbol;
break;
case 5:
board[2][2] = symbol;
break;
case 6:
board[2][4] = symbol;
break;
case 7:
board[4][0] = symbol;
break;
case 8:
board[4][2] = symbol;
break;
case 9:
board[4][4] = symbol;
break;
default:
break;
}
}
public static String Winner(){
List topRow = Arrays.asList(1, 2, 3);
List middleRow = Arrays.asList(4, 5, 6);
List bottomRow = Arrays.asList(7, 8, 9);
List topCol = Arrays.asList(1, 4, 7);
List middleCol = Arrays.asList(2, 5, 8);
List bottomCol = Arrays.asList(3, 6, 9);
List cross1 = Arrays.asList(1, 5, 9);
List cross2 = Arrays.asList(7, 5, 3);
List<List>winningConditions = new ArrayList<List>();
winningConditions.add(topRow);
winningConditions.add(middleRow);
winningConditions.add(bottomRow);
winningConditions.add(topCol);
winningConditions.add(middleCol);
winningConditions.add(bottomCol);
winningConditions.add(cross1);
winningConditions.add(cross2);
for (List l:winningConditions ){
if (playerPositions.containsAll(l)){
return "you have won!!! ";
}else if (computerPositions.containsAll(l)){
return "you lost to the computer";
}else if (playerPositions.size()+computerPositions.size() ==9){
return "Tie!";
}
}
return "";
}
}