Tic Tac Toe

//Tony Piechowski
//this tic tac toe kinda sucks... but it works
package tictactoe;

import java.util.Scanner;

public class TicTacToe {
   
    private String[][]board;
    private static final int ROWS = 3;
    private static final int COLS = 3;
   
    public TicTacToe() {
        board = new String[ROWS][COLS];
        for (int i = 0; i< ROWS; i++){
            for (int j = 0; j< COLS; j++){
                board[i][j] = " ";
            }
        }
    }
   
    public void set (int i, int j, String player){
        if (board[i][j].equals(" ")){
            board[i][j] = player;
        }
    }
   
    public String toString(){
        String r = "";
        for (int i = 0; i < ROWS; i++){
            r = r + "|";
            for (int j = 0; j < COLS; j++){
                r = r + board[i][j];
            }
            r = r + "| \n";
        }
        return r;
    }
  
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String player = "x";
       
        TicTacToe game = new TicTacToe();
        boolean done = false;
        while (done == false){
            System.out.println(game.toString());
            System.out.print("row for " + player + " (-i to exit): ");
            System.out.println();
            int rows = in.nextInt();
            System.out.println();
            if (rows < 0 ){
                done = true;
            }
            else{
                System.out.print("Column for " + player + ": ");
                System.out.println();
                int cols = in.nextInt();
                System.out.println();
                game.set(rows, cols, player);
                if (player.equals("x")){
                    player = "o";
                }
                else {
                    player = "x";
                }
            }
        }
    }
   
}



This site is powered by the Northwoods Titan Content Management System