'''0 public class''TestGame {
'''1 ''''public''static void''main(String[] args)''{
'''2 ''''''''Board board''= new''Board();
'''3 ''''''''Input input''= new''Input();
'''4 '' '' '' ''
'''5 ''''''''gameLoop(board, input);
'''6 ''''''''Output.winner(board.checkForWinner());
'''7 ''''}
'''8 '' ''
'''9 ''''public''static void''gameLoop(Board board,''Input input)''{
''10 ''''''''int moves''= 0;
''11 '' '' '' ''
''12 ''''''''while''(true) {
''13 ''''''''''''if''(board.checkForWinner() !=''0 ||''moves >=''9) break;
''14 '' '' '' '' '' ''
''15 ''''''''''''Output.drawBoard(board);
''16 '' '' '' '' '' ''
''17 ''''''''''''while''(true) {
''18 ''''''''''''''''int[]''coordinates =''input.getCoordinates();
''19 '' '' '' '' '' '' '' ''
''20 ''''''''''''''''if (board.setMark('X',''coordinates[0], coordinates[1]))''{
''21 ''''''''''''''''''''moves++;
''22 ''''''''''''''''''''break;
''23 ''''''''''''''''}
''24 ''''''''''''}
''25 '' '' '' '' '' ''
''26 ''''''''''''if (board.checkForWinner()''!= 0''|| moves''>= 9)''break;
''27 '' '' '' '' '' ''
''28 ''''''''''''while (true)''{
''29 ''''''''''''''''int x''= (int)''Math.round(Math.random() *''2);
''30 ''''''''''''''''int y''= (int)''Math.round(Math.random() *''2);
''31 '' '' '' '' '' '' '' ''
''32 ''''''''''''''''if (board.setMark('O',''x, y))''{
''33 ''''''''''''''''''''Output.drawBoard(board);
''34 ''''''''''''''''''''Output.opponentTurn(x, y);
''35 ''''''''''''''''''''moves++;
''36 ''''''''''''''''''''break;
''37 ''''''''''''''''}
''38 ''''''''''''}
''39 ''''''''}
''40 '' '' '' ''
''41 ''''''''Output.drawBoard(board);
''42 ''''}
''43 }
''44
''45 public''class Board''{
''46 ''''private char[][]''board =''new char[3][3];
''47 '' ''
''48 ''''public''boolean setMark(char''mark, int''x, int''y) {
''49 ''''''''if''(board[y][x] ==''0) {
''50 ''''''''''''''board[y][x] =''mark;
''51 ''''''''''''return true;
''52 ''''''''}
''53 '' '' '' ''
''54 ''''''''return''false;
''55 ''''}
''56 '' ''
''57 ''''public boolean''setMark(char mark,''int[] xy)''{
''58 ''''''''int x''= xy[0];
''59 ''''''''int''y =''xy[1];
''60 '' '' '' ''
''61 ''''''''if (board[y][x]''== 0)''{
''62 ''''''''''''board[y][x] =''mark;
''63 ''''''''''''return true;
''64 ''''''''}
''65 '' '' '' ''
''66 ''''''''return''false;
''67 ''''}
''68 '' ''
''69 ''''public char''getMark(int x,''int y)''{
''70 ''''''''if (board[y][x]''== 0)''return ' ';
''71 ''''''''return''board[y][x];
''72 ''''}
''73 '' ''
''74 ''''public char''checkForWinner() {
''75 ''''''''for''(int x''= 0;''x <''3; x++)''{
''76 ''''''''''''if (board[0][x]''== board[1][x]''&& board[0][x]''== board[2][x])''return board[0][x];
''77 ''''''''}
''78 '' '' '' ''
''79 ''''''''for''(int y''= 0;''y <''3; y++)''{
''80 ''''''''''''if (board[y][0]''== board[y][1]''&& board[y][0]''== board[y][2])''return board[y][0];
''81 ''''''''}
''82 '' '' '' ''
''83 ''''''''if''(board[0][0] ==''board[1][1] &&''board[0][0] ==''board[2][2]) return''board[0][0];
''84 '' '' '' ''
''85 ''''''''if (board[2][0]''== board[1][1]''&& board[2][0]''== board[0][2])''return board[2][0];
''86 '' '' '' ''
''87 ''''''''return''0;
''88 ''''}
''89 }
''90
''91 public class''Input {
''92 ''''Scanner''scanner =''new Scanner(System.in);
''93 '' ''
''94 ''''public''int[] getCoordinates()''{
''95 ''''''''System.out.println();
''96 ''''''''System.out.print('Make your move with numpad and enter: ');
''97 ''''''''String move''= this.scanner.nextLine();
''98 ''''''''return''Numpad.getXY(move);
''99 ''''}
'100 }
'101
'102 public class''Numpad {
'103 ''''public''static int[]''getXY(String input)''{
'104 ''''''''switch (input)''{
'105 ''''''''''''case '1':''return new''int[] {0,''2};
'106 ''''''''''''case '2':''return new''int[] {1,''2};
'107 ''''''''''''case '3':''return new''int[] {2,''2};
'108 ''''''''''''case '4':''return new''int[] {0,''1};
'109 ''''''''''''case '5':''return new''int[] {1,''1};
'110 ''''''''''''case '6':''return new''int[] {2,''1};
'111 ''''''''''''case '7':''return new''int[] {0,''0};
'112 ''''''''''''case '8':''return new''int[] {1,''0};
'113 ''''''''''''case '9':''return new''int[] {2,''0};
'114 ''''''''}
'115 '' '' '' ''
'116 ''''''''return null;
'117 ''''}
'118 }
'119
'120 public''class Output''{
'121 ''''public static''void drawBoard(Board''board) {
'122 ''''''''System.out.println();
'123 ''''''''for''(int y''= 0;''y <''5; y++)''{
'124 ''''''''''''for (int''x =''0; x''< 5;''x++) {
'125 ''''''''''''''''if''(y %''2 ==''1) {
'126 ''''''''''''''''''''System.out.print('-');
'127 ''''''''''''''''}''else {
'128 ''''''''''''''''''''if''(x %''2 ==''1) {
'129 ''''''''''''''''''''''''System.out.print('|');
'130 ''''''''''''''''''''}''else {
'131 ''''''''''''''''''''''''System.out.print(board.getMark(x''/ 2,''y /''2));
'132 ''''''''''''''''''''}
'133 ''''''''''''''''}
'134 ''''''''''''}
'135
'136 ''''''''''''System.out.println();
'137 ''''''''}
'138 ''''}
'139 '' ''
'140 ''''public static''void opponentTurn(int''x, int''y) {
'141 ''''''''System.out.println();
'142 ''''''''System.out.print('Opponent placed O to '''+ (x''+ 1)''+ '/'''+ (y''+ 1));
'143 ''''''''System.out.println();
'144 ''''}
'145 '' ''
'146 ''''public''static void''winner(char mark)''{
'147 ''''''''if (mark''== 0)''System.out.println('Draw!');
'148 ''''''''else System.out.println(mark''+ ' wins!');
'149 ''''}
'150 }