Guest User

Tic Tac Toe

a guest
Dec 14th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. public class Game {
  4.    
  5.     public static void main(String[]args){
  6.        
  7.         System.out.println("Welcome to Tyer's version of Tic Tac Toe.");
  8.         System.out.println("You will be represented by the letter x.");
  9.         Input();
  10.     }
  11.     public static void Input(){
  12.         int score = 0;
  13.         while(score < 7){
  14.         System.out.print("What row would you like to go in: ");
  15.         Scanner rowReader = new Scanner(System.in);//row reader
  16.         int row = Integer.parseInt(rowReader.nextLine());
  17.         System.out.print("What column would you like to go in: ");
  18.         Scanner columnReader = new Scanner(System.in);//column reader
  19.         int column = Integer.parseInt(columnReader.nextLine());
  20.        
  21.         String[][] cell = new String[3][3]; //initialize array
  22.         for(int i = 0; i < cell.length; i++) {
  23.             for(int j = 0; j < cell[i].length; j++) {
  24.                 cell[i][j] = " ";
  25.             }
  26.         }
  27.  
  28.            
  29.             cell[row - 1][column - 1] = "X";
  30.            
  31.             String newLine = System.getProperty("line.separator");
  32.             String board = (" " + cell[0][0] + " | " + cell[0][1] + " | " + cell[0][2] + " " + newLine +
  33.             "--- --- ---" + newLine +
  34.             " " + cell[1][0] + " | " + cell[1][1] + " | " + cell[1][2] + " " + newLine +
  35.             "--- --- ---" + newLine +
  36.             " " + cell[2][0] + " | " + cell[2][1] + " | " + cell[2][2] + " " + newLine);
  37.            
  38.             System.out.print(board);
  39.  
  40.         if (0 > row && row > 4 && 0 > column && column > 4){
  41.             System.out.println("Sorry, your input value was invalid.");
  42.         }
  43.             score++;
  44.          }
  45.        
  46.             }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment