Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import java.awt.Color;
  3. import java.util.Scanner;
  4.  
  5. public class GameBoard
  6. {
  7.    JFrame win = new JFrame("Tic Tac Toe");
  8.    public GameBoard()
  9.    {    
  10.        
  11.        win.setBounds(10, 10, 195, 215);
  12.        win.setLayout(null);
  13.        win.setVisible(true);
  14.  
  15.        Rectangle[][] GameBoard = new Rectangle[3][3];
  16.        final int SIZE = 61;
  17.        int X_position = 60;
  18.        int Y_position = 60;
  19.              
  20.        for (int row=0; row <= GameBoard.length -1; row++){
  21.            for (int col=0; col <= GameBoard[row].length -1; col ++){    
  22.                 GameBoard[row][col] = new Rectangle((X_position *row), (Y_position *col), SIZE, SIZE);
  23.                     win.add(GameBoard[row][col]);
  24.                             if (((row + col) % 2) == 0)
  25.                             (GameBoard[row][col]).setBackground(Color.red);
  26.                             else
  27.                             (GameBoard[row][col]).setBackground(Color.black);}}      
  28.        win.repaint();
  29.        userInput();
  30.    }
  31.    
  32.    public void userInput()
  33.    {
  34.        Scanner keyboard = new Scanner(System.in);
  35.            
  36.        Oval[][] Player1 = new Oval[3][3];
  37.        Oval[][] Player2 = new Oval[3][3];
  38.        
  39.        int TempIntRow = 0;
  40.        int TempIntCol = 0;
  41.        
  42.        int X_position = 60;
  43.        int Y_position = 60;
  44.        
  45.        int PlayerTwoInput =0;
  46.        
  47.        final int OVAL_SIZE = 61;
  48.        //Simple way to preform the same action?
  49.        //Perhaps a clever IF statement?
  50.        //Error checking required?
  51.        for (int PlayerOneInput=0; PlayerOneInput <= 4; PlayerOneInput++)
  52.            {
  53.                //Player One
  54.                System.out.println("Round: " + PlayerOneInput);
  55.                System.out.println("Player One: enter row (0-1-2):");
  56.                TempIntRow = (keyboard.nextInt();
  57.                
  58.                System.out.println("Player One: enter column (0-1-2):");
  59.                TempIntCol = keyboard.nextInt();
  60.                Player1[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
  61.                Player1[TempIntRow][TempIntCol].setBackground(Color.blue);
  62.              
  63.                win.add((Player1[TempIntRow][TempIntCol]), 0);
  64.                win.repaint();
  65.                //Player Two
  66.                if (PlayerTwoInput <= 3)
  67.             {
  68.                System.out.println("Player Two: enter row (0-1-2):");
  69.                TempIntRow = keyboard.nextInt();
  70.                
  71.                System.out.println("Player Two: enter column (0-1-2):");
  72.                TempIntCol = keyboard.nextInt();
  73.                Player2[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
  74.                Player2[TempIntRow][TempIntCol].setBackground(Color.white);
  75.  
  76.                win.add((Player2[TempIntRow][TempIntCol]), 0);
  77.                win.repaint();
  78.                PlayerTwoInput ++;
  79.             }
  80.                else
  81.                win.repaint();
  82.             }    
  83.            System.out.print("Thankyou for playing :)");
  84.            keyboard.close();
  85.        }
  86.  
  87.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement