Tic-Tac-Toe

Want to make video games, but have limited programing skills? Then start here. We’ll explore a simple Tic-Tac-Toe that runs exclusively through your terminal or command prompt.

First, here are the pieces used to assemble this project:

  • Scanner
  • Arrays
  • Loops
  • Conditional statements
  • Methods

I decided that I wanted to build the game in steps and divided it into different sections to make programing it easier, and have the code more human readable. Shown below are the global variables used and the main method.

public class TicTacToe_v1 {

  //Global variables that need to be accessed by different methods
  private static String[][] playBoard = { { " ", " ", " " }, { " ", " ", " " }, { " ", " ", " " } };
  private static boolean play;

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    welcome();

    //Checking if person wants to play or needs help
    if (input.next().toLowerCase().equals("play")) {
      play = true;
      while (play) {
        play(input);
      }

    } else {
      help();
    }

    input.close();

  }

Looks fairly simple. After making a Scanner object to receive user input we call to the welcome method. It prints out a “title screen”, and prompts the user with whether they would like to play or need help.

private static void welcome() {
  System.out.println(" _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");
  System.out.println("|_______  __   _______       __   _______               |");
  System.out.println("|   |  o /        |    __   /        |  ___   __        |");
  System.out.println("|   |  | |   ---  |   (  |  |   ---  | (   ) /__\\       |");
  System.out.println("|   |  | \\__      |    \\_|\\ \\__      | (___) \\__  v1.0  |");
  System.out.println("|_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_|");
  System.out.print("\nPlay or Help?: ");
}

The program then waits and then checks the user input to see if they typed in play or not. If they did it sets the control variable play to true, and starts a game. If not, the help method is called and prints of its spiel, then the program ends.

The play methods main action is the following for loop.

private static void play(Scanner input) {
String string;

for (int i = 0; i < 9; i++) {

  //Checking whose turn it is
  if (i % 2 == 0) {
    string = "X";
  } else {
    string = "O";
  }

  //Taking a turn for that person
  turn(string, input);
  
  //Checking if that person won
  if (win(string)) {
    System.out.println(string + " Won!");
    break;
  }
  //No spaces left
  if (i == 8) {
    System.out.println("Draw...");
  }
}

It will only run nine times (the max number of possible turns) taking one turn each time. Each run through first figures out whose turn it is, X or O. This is done by looking at weather the turn is even or odd. Even turns are X and odd is O. Next an actual turn is taken by that character. Then it checks to see if that person has won or not. Lastly, it checks to see if that was the last turn. If it was it states it’s a draw.

The turn method does three things. One is find the position the person would like to place their marker in, checks if that space is available, and placing the marker if it is, and asking for a new spot if it isn’t.

private static void turn(String string, Scanner input) {
    byte row;
    byte column;

    System.out.print("What row would you like to place your " + string + "?: ");
    row = input.nextByte();

    System.out.print("What column would you like to place your " + string + "?: ");
    column = input.nextByte();

    //Checking if spot is open
    if (playBoard[row - 1][column - 1].equals(" ")) {
      playBoard[row - 1][column - 1] = string;
      printBoard();
    } else {
      System.out.println("Spot already taken! Try again...");
      turn(string, input);
    }

  }

Lastly the win condition checks every possible way a person could win after every turn. This is done simply by having a series of conditional statements that look to see if their line of three has all the same marker.

Find the whole code here!

TicTacToe_v1