import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Hashtable; public class TicTacToe { public static void main(String[] args) { TicTacToe now=new TicTacToe(); now.startMatch(); } private int[][] marks; private int[][] wins; private int[] weights; private char[][] grid; private final int knotcount=3; private final int crosscount=4; private final int totalcount=5; private final int playerid=0; private final int compid=1; private final int truceid=2; private final int playingid=3; private String movesPlayer; private byte override; private char[][] overridegrid={{'o','o','o'},{'o','o','o'},{'o','o','o'}}; private char[][] numpad={{'7','8','9'},{'4','5','6'},{'1','2','3'}}; private Hashtable crossbank; private Hashtable knotbank; public void startMatch() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Start?(y/n):"); char choice='y'; try { choice=br.readLine().charAt(0); } catch(Exception e) { System.out.println(e.getMessage()); } if(choice=='n'||choice=='N') { return; } System.out.println("Use a standard numpad as an entry grid, as so:\n "); display(numpad); System.out.println("Begin"); int playerscore=0; int compscore=0; do { int result=startGame(); if(result==playerid) playerscore++; else if(result==compid) compscore++; System.out.println("Score: Player-"+playerscore+" AI-"+compscore); System.out.print("Another?(y/n):"); try { choice=br.readLine().charAt(0); } catch(Exception e) { System.out.println(e.getMessage()); } }while(choice!='n'||choice=='N'); System.out.println("Game over."); } private void put(int cell,int player) { int i=-1,j=-1;; switch(cell) { case 1:i=2;j=0;break; case 2:i=2;j=1;break; case 3:i=2;j=2;break; case 4:i=1;j=0;break; case 5:i=1;j=1;break; case 6:i=1;j=2;break; case 7:i=0;j=0;break; case 8:i=0;j=1;break; case 9:i=0;j=2;break; default:display(overridegrid);return; } char mark='x'; if(player==0) mark='o'; grid[i][j]=mark; display(grid); } private int startGame() { init(); display(grid); int status=playingid; while(status==playingid) { put(playerMove(),0); if(override==1) { System.out.println("O wins."); return playerid; } status=checkForWin(); if(status!=playingid) break; try{Thread.sleep(1000);}catch(Exception e){System.out.print(e.getMessage());} put(compMove(),1); status=checkForWin(); } return status; } private void init() { movesPlayer=""; override=0; marks=new int[8][6]; wins=new int[][] //new int[8][3]; { {7,8,9}, {4,5,6}, {1,2,3}, {7,4,1}, {8,5,2}, {9,6,3}, {7,5,3}, {9,5,1} }; weights=new int[]{3,2,3,2,4,2,3,2,3}; grid=new char[][]{{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; crossbank=new Hashtable(); knotbank=new Hashtable(); } private void mark(int m,int player) { for(int i=0;imax) { max=weights[i]; cell=i+1; } //This section ensures the computer never loses //Remove it for a fair match //Dirty kluge if(movesPlayer.equals("76")||movesPlayer.equals("67")) cell=9; else if(movesPlayer.equals("92")||movesPlayer.equals("29")) cell=3; else if (movesPlayer.equals("18")||movesPlayer.equals("81")) cell=7; else if(movesPlayer.equals("73")||movesPlayer.equals("37")) cell=4*((int)(Math.random()*2)+1); else if(movesPlayer.equals("19")||movesPlayer.equals("91")) cell=4+2*(int)(Math.pow(-1, (int)(Math.random()*2))); mark(cell,1); fixWeights(); crossbank.put(cell, 0); return cell; } private int playerMove() { System.out.print("What's your move?: "); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int cell=0; int okay=0; while(okay==0) { try { cell=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println(e.getMessage()); } if(cell==7494) { override=1; return -1; } if((cell<1||cell>9)||weights[cell-1]==Integer.MIN_VALUE) System.out.print("Invalid move. Try again:"); else okay=1; } playerMoved(cell); System.out.println(); return cell; } private void playerMoved(int cell) { movesPlayer+=cell; mark(cell,0); fixWeights(); knotbank.put(cell, 0); } private int checkForWin() { int crossflag=0,knotflag=0; for(int i=0;i