Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import java.awt.Color;
- import java.util.Scanner;
- public class GameBoard
- {
- JFrame win = new JFrame("Tic Tac Toe");
- public GameBoard()
- {
- win.setBounds(10, 10, 195, 215);
- win.setLayout(null);
- win.setVisible(true);
- Rectangle[][] GameBoard = new Rectangle[3][3];
- final int SIZE = 61;
- int X_position = 60;
- int Y_position = 60;
- for (int row=0; row <= GameBoard.length -1; row++){
- for (int col=0; col <= GameBoard[row].length -1; col ++){
- GameBoard[row][col] = new Rectangle((X_position *row), (Y_position *col), SIZE, SIZE);
- win.add(GameBoard[row][col]);
- if (((row + col) % 2) == 0)
- (GameBoard[row][col]).setBackground(Color.red);
- else
- (GameBoard[row][col]).setBackground(Color.black);}}
- win.repaint();
- userInput();
- }
- public void userInput()
- {
- Scanner keyboard = new Scanner(System.in);
- Oval[][] Player1 = new Oval[3][3];
- Oval[][] Player2 = new Oval[3][3];
- int TempIntRow = 0;
- int TempIntCol = 0;
- int X_position = 60;
- int Y_position = 60;
- int PlayerTwoInput =0;
- final int OVAL_SIZE = 61;
- //Simple way to preform the same action?
- //Perhaps a clever IF statement?
- //Error checking required?
- for (int PlayerOneInput=0; PlayerOneInput <= 4; PlayerOneInput++)
- {
- //Player One
- System.out.println("Round: " + PlayerOneInput);
- System.out.println("Player One: enter row (0-1-2):");
- TempIntRow = (keyboard.nextInt();
- System.out.println("Player One: enter column (0-1-2):");
- TempIntCol = keyboard.nextInt();
- Player1[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
- Player1[TempIntRow][TempIntCol].setBackground(Color.blue);
- win.add((Player1[TempIntRow][TempIntCol]), 0);
- win.repaint();
- //Player Two
- if (PlayerTwoInput <= 3)
- {
- System.out.println("Player Two: enter row (0-1-2):");
- TempIntRow = keyboard.nextInt();
- System.out.println("Player Two: enter column (0-1-2):");
- TempIntCol = keyboard.nextInt();
- Player2[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
- Player2[TempIntRow][TempIntCol].setBackground(Color.white);
- win.add((Player2[TempIntRow][TempIntCol]), 0);
- win.repaint();
- PlayerTwoInput ++;
- }
- else
- win.repaint();
- }
- System.out.print("Thankyou for playing :)");
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement