Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Project #4
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 12:19 pm on Friday, April 3


The following code was given in class on Friday, March 27th. It is the beginning of a program that supports command-line based play of tic-tac-toe for two human players (no AI support is provided nor necessary). The code is complete, except that the implementation of the legal() and checkWinner methods are given as prototypes only.

import java.util.Scanner;

public class TicTacToe {
    private static final int X = 1, O = -1, NONE = 0;
    // constant values
    
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        
        int [][] board = new int[3][3];
        
        initBoard(board);
        
        int turn = X;
        int winner = NONE;
        
        while (winner == NONE) {
            printBoard(board);
            
            System.out.print("Move (row [1-3] column [1-3]:"  );
            int row = inp.nextInt();
            int col = inp.nextInt();
            
            if (legal(board, row, col)) {
                winner = play(board,row,col, turn);
                turn = turn * -1;  // Works because X is 1 and Y is -1
            } else {
                System.out.println("Not a legal move.");
            }
            
        }
        
    }// main
    
    public static void initBoard(int[][] bd) {
        for(int i = 0; i < bd.length; i++) {
            for (int j = 0; j < bd[i].length; j++) {
                bd[i][j] = NONE;
            }
        }
    }
    
    public static String disp(int i) {
        if (i == X) {
            return "X";
        } else if (i == O) {
            return "O";
        } else {
            return ".";
        }
    }
    
    public static void printBoard(int[][] bd) {
        for (int i = 0; i < bd.length;  i = i + 1) {
            for (int j = 0; j < bd[i].length; j = j + 1) {
                System.out.print(" " + disp(bd[i][j]) + " ");
                if (j < bd[0].length - 1) {
                    System.out.print(" | ");
                } else {
                    System.out.println();
                }
            }
            if (i < bd.length - 1) {
                System.out.println("--------------");
            } else {
                System.out.println();
            }
        }
    }
    
    public static boolean legal (int[][] board, int row, int col) {
  
       return true;
        
    }
    
    /**
     *  Places the piece corresponding to turn at the specified row and 
     *  col values in board.
     *  Precondition:  legal(board, row, col) returns true;
     */
    public static int play(int[][] board, int row, int col, int turn) {
           
        board[row-1][col-1] = turn;
        // Because array arguments are passed to methods by their
        // addresses (they're aliases), all changes in this method
        // affect the argument from the orginal call to play().
        
        return checkWinner(board);
    }
    
    public static int checkWinner(int[][] bd) {
        // Returns X, O, or NONE
        
        return NONE;  // prototype:  the correct version is left as an exercise
        
        // look at each row

        // look at each column
        
        // look top left to bottom right
        
        // look bottom left to top right
        
    }
} // class TicTacToe

Your Job

  1. Complete the implementations of legal() and checkWinner, so that the program supports correct play of the traditional version of tic-tac-toe.

  2. Make a second version of the program, for which the game board is some dimension greater than 3, and the number of in-a-row necessary for a win is some value less than the board's dimension. For example, you might have a 4x4 board for which a win is still three in a row, or a 5x5 board which requires 3 in a row to win (or 4 in a row). The choice for both dimension and win size is up to you, but don't make it too large, as that will prove very difficult.

Summary of Expectations

Turn In

John H. E. Lasseter