Skip to content

Instantly share code, notes, and snippets.

@toydotgame
Created December 18, 2020 07:25
Show Gist options
  • Save toydotgame/c7cf7fadbc9e7664ee6b93cd99a38759 to your computer and use it in GitHub Desktop.
Save toydotgame/c7cf7fadbc9e7664ee6b93cd99a38759 to your computer and use it in GitHub Desktop.
A simple command-line Tic Tac Toe game.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
static ArrayList<Integer> playerPositions = new ArrayList<Integer>();
static ArrayList<Integer> cpuPositions = new ArrayList<Integer>();
@SuppressWarnings("unlikely-arg-type")
public static void main(String[] args) {
char[][] gameBoard = {
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}
};
printGameBoard(gameBoard);
while(true) {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.print("Enter your placement (1-9): ");
int playerPos = scan.nextInt();
while(playerPositions.contains(playerPos) || cpuPositions.contains(playerPositions)) {
System.out.print("Position taken. Please choose a valid one: ");
playerPos = scan.nextInt();
}
placePiece(gameBoard, playerPos, "player");
String result = checkWinner();
if(result.length() > 0) {
System.out.println(result);
break;
}
/* This is the computer's "AI", which makes its moves.
* No, I'm not going to _actually_ add an AI system in this, ever.
* That's too much work for a silly Tic Tac Toe game...
* Java's _Random_ library will have to do for now. ;) */
Random rand = new Random();
int cpuPos = rand.nextInt(9) + 1;
while(playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
cpuPos = rand.nextInt(9) + 1;
}
placePiece(gameBoard, cpuPos, "cpu");
printGameBoard(gameBoard);
if(result.length() > 0) {
System.out.println(result);
break;
}
}
}
public static void printGameBoard(char[][] gameBoard) {
for(char[] row : gameBoard) {
for(char c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static void placePiece(char[][] gameBoard, int pos, String user) {
char symbol = ' ';
if(user.equals("player")) {
symbol = 'X';
playerPositions.add(pos);
} else if(user.equals("cpu")) { // People say "CPU" for the computer in these games. But this is Java, so I might as well say RAM. :P
symbol = 'O';
cpuPositions.add(pos);
}
switch(pos) {
case 1:
gameBoard[0][0] = symbol;
break;
case 2:
gameBoard[0][2] = symbol;
break;
case 3:
gameBoard[0][4] = symbol;
break;
case 4:
gameBoard[2][0] = symbol;
break;
case 5:
gameBoard[2][2] = symbol;
break;
case 6:
gameBoard[2][4] = symbol;
break;
case 7:
gameBoard[4][0] = symbol;
break;
case 8:
gameBoard[4][2] = symbol;
break;
case 9:
gameBoard[4][4] = symbol;
break;
default:
break;
}
}
@SuppressWarnings("rawtypes")
public static String checkWinner() {
List topRow = Arrays.asList(1, 2, 3);
List midRow = Arrays.asList(4, 5, 6); // Middle row. "mid" instead of "middle" because formatting look pretty ooooo.
List botRow = Arrays.asList(7, 8, 9); // Bottom row. Same reason. oooOOOoooo
List leftCol = Arrays.asList(1, 4, 7);
List midCol = Arrays.asList(2, 5, 8);
List rightCol = Arrays.asList(3, 6, 9); // Yeah I guess the columns messed up my nice formatting...
List diag1 = Arrays.asList(1, 5, 9);
List diag2 = Arrays.asList(7, 5, 3); // Hmm the diagonals are better...
List<List> winningConditions = new ArrayList<List>();
winningConditions.add(topRow);
winningConditions.add(midRow);
winningConditions.add(botRow);
winningConditions.add(leftCol);
winningConditions.add(midCol);
winningConditions.add(rightCol);
winningConditions.add(diag1);
winningConditions.add(diag2);
for(List l : winningConditions) {
if(playerPositions.containsAll(l)) {
return "You won!";
} else if(cpuPositions.containsAll(l)) {
return "You lost. :(";
} else if(playerPositions.size() + cpuPositions.size() == 9) {
return "It's a tie.";
}
}
return ""; // This is a lie. You have been scammed.
// (It also has a length of zero thus helps out some code above...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment