import tio.*; /* Noughts and Crosses * Rudimentary implementation showing just some * of the basic classes and basic methods. * * The implementation below uses only primitive programming * techniques. * * Exercise: Extend this implementation by a nice * graphical user interface. * * Simplest main() method using this game: * * class NandC{ * * public static void main(String[] args) { * * Game g = new Game(3); * g.play(); * * } * } * */ class Game{ private static Player nPlayer = new Player(new Token("Nought","O")); private static Player cPlayer = new Player(new Token("Cross","X")); private static Grid grid; private Impair impair; Game(int s){ grid = new Grid(s); impair = new Impair(grid,nPlayer,cPlayer);} void display(){grid.display();} void place(int w, int h, Player p) throws NotOnGridException,CellOccupiedException{ try{ grid.place(w,h,p.getToken()); impair.update(w,h,p); } catch(NotOnGridException noge){throw noge;} catch(CellOccupiedException coe){throw coe;} } Player getNPlayer(){return nPlayer;} Player getCPlayer(){return cPlayer;} Grid getGrid(){return grid;} Impair getImpair(){return impair;} Winner threeInARow(){return impair.threeInARow();} void play(){/* Very simple and primitve play method */ boolean won = false; boolean nPlays = false; int length = grid.getSize()*grid.getSize(); int i = 1; display(); while(!won && i <= length){ boolean empty = false; while(!empty){ try{ empty = true; int w = Console.in.readInt(); int h = Console.in.readInt(); if(nPlays) place(w,h,nPlayer); else place(w,h,cPlayer); } catch(CellOccupiedException coe){ empty = false; display(); } catch(NotOnGridException noge){ empty = false; display(); } } nPlays = !nPlays; i++; won = threeInARow().isWon(); display(); } Winner w = threeInARow(); if(won) System.out.println(w.getPlayer().getType()+" won the game!"); else System.out.println("Draw!"); } /* More methods to come */ } /* * Class Token * */ class Token{ private String name; private String symbol; Token(String n, String s){ name = n; symbol = s; } String getType(){return name;} String getSymbol(){return symbol;} } /* * Class Player * */ class Player{ private Token token; Player(Token t){token = t;} String getType(){return token.getType();} String getSymbol(){return token.getSymbol();} Token getToken(){return token;} } /* * Class Impair * */ class Impair{ Grid grid; int size; Player n; Player c; int[][] rows; int[][] columns; int[] diagonalOne; int[] diagonalTwo; Impair(Grid g, Player n, Player c){ grid = g; size = grid.getSize(); this.n = n; this.c = c; rows = new int[grid.getSize()][2]; columns = new int[grid.getSize()][2]; diagonalOne = new int[2]; diagonalTwo = new int[2]; } void update(int w, int h, Player p){ int player = 1; if(p==n) player = 0; rows[h][player]++; columns[w][player]++; if(w==h)diagonalOne[player]++; if(size-w==size-h)diagonalTwo[player]++; } Winner threeInARow(){ int r = -1; boolean found = false; int i; /* Rows */ i=0; while(!found && i=0; i--){ // print a line System.out.print("|"); for(int j = 0; j