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];
- char[][] GameBoardInput = new char[3][3];
- int TempIntRow = 0;
- int TempIntCol = 0;
- int X_position = 60;
- int Y_position = 60;
- int PlayerTwoInput =0;
- final int OVAL_SIZE = 61;
- GameBoardInput[0][0]='-';//initializes the board with the characters 1-9
- GameBoardInput[0][1]='-';
- GameBoardInput[0][2]='-';
- GameBoardInput[1][0]='-';
- GameBoardInput[1][1]='-';
- GameBoardInput[1][2]='-';
- GameBoardInput[2][0]='-';
- GameBoardInput[2][1]='-';
- GameBoardInput[2][2]='-';
- //Simple way to preform the same action?
- //Perhaps a clever for statement?
- //Error checking required?
- for (int PlayerOneInput=0; PlayerOneInput <= 4; PlayerOneInput++)
- {
- //Player One
- displayBoard(GameBoardInput);
- do
- {
- System.out.println("Round: " + ((PlayerOneInput) +1) );
- System.out.println("Player One 'X': enter row (1-2-3):");
- if ((keyboard.nextInt()) < 1 || (keyboard.nextInt()) > 3)
- {
- TempIntRow = (invalidInput(TempIntRow) -1);
- }
- else
- TempIntRow = (keyboard.nextInt() -1);
- System.out.println("Player One: enter column (1-2-3):");
- if ((keyboard.nextInt()) < 1 || (keyboard.nextInt()) > 3)
- {
- TempIntCol = (invalidInput(TempIntRow) -1);
- }
- else
- TempIntCol = (keyboard.nextInt() -1);
- } while (checkValidInput(GameBoardInput, TempIntRow, TempIntCol) == false);
- //Check if placement is valid using GameBoradInput[][]
- //If valid proceed with placement
- //Else ask user to re-input
- //Divide users into different methods?
- 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)
- {
- do
- {
- System.out.println("Player Two '0': enter row (1-2-3):");
- TempIntRow = keyboard.nextInt();
- System.out.println("Player Two: enter column (1-2-3):");
- TempIntCol = keyboard.nextInt();
- } while (checkValidInput(GameBoardInput, TempIntRow, TempIntCol) == false);
- //GameBoard
- 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();
- }
- //Call Check for Winner
- //Pass gameboard array to method as argument
- System.out.print("Thankyou for playing :)");
- keyboard.close();
- }
- public int invalidInput(int TempInt)
- {
- do
- {
- System.out.println("INVALID INPUT: Please try again");
- TempInt = keyboard.nextInt();
- }
- while ((TempInt < 1 || TempInt > 3));
- return TempInt;
- }
- public static boolean checkValidInput(char[][] TestArray,int TempIntRow, int TempIntCol)
- {
- if (TestArray[TempIntRow][TempIntCol] == '-')
- return true;
- else
- System.out.println("INVALID INPUT!: please try again :) ");
- return false;
- }
- public static void displayBoard(char[][] a)
- {
- //Fixed formatting to display in console properly
- System.out.println(a[0][0]+" "+a[0][1]+" "+a[0][2]+"\n"+
- "_ _ _ \n\n"+
- a[1][0]+" "+a[1][1]+" "+a[1][2]+"\n"+
- "_ _ _ \n\n"+
- a[2][0]+" "+a[2][1]+" "+a[2][2]+"\n");
- }
- public static boolean checkForWinner(char[][] a, String nameX,
- String nameO, int count)
- {
- //executes if "x" wins
- if((a[0][0]=='x' && a[0][1]=='x' && a[0][2]=='x') ||
- (a[1][0]=='x' && a[1][1]=='x' && a[1][2]=='x') ||
- (a[2][0]=='x' && a[2][1]=='x' && a[2][2]=='x') ||
- (a[0][0]=='x' && a[1][0]=='x' && a[2][0]=='x') ||
- (a[0][1]=='x' && a[1][1]=='x' && a[2][1]=='x') ||
- (a[0][2]=='x' && a[1][2]=='x' && a[2][2]=='x') ||
- (a[0][0]=='x' && a[1][1]=='x' && a[2][2]=='x') ||
- (a[2][0]=='x' && a[1][1]=='x' && a[0][2]=='x'))
- {
- System.out.println(nameX + " is the winner");
- System.out.println();
- return false;
- }
- //executes if "o" wins
- else if((a[0][0]=='o' && a[0][1]=='o' && a[0][2]=='o') ||
- (a[1][0]=='o' && a[1][1]=='o' && a[1][2]=='o') ||
- (a[2][0]=='o' && a[2][1]=='o' && a[2][2]=='o') ||
- (a[0][0]=='o' && a[1][0]=='o' && a[2][0]=='o') ||
- (a[0][1]=='o' && a[1][1]=='o' && a[2][1]=='o') ||
- (a[0][2]=='o' && a[1][2]=='o' && a[2][2]=='o') ||
- (a[0][0]=='o' && a[1][1]=='o' && a[2][2]=='o') ||
- (a[2][0]=='o' && a[1][1]=='o' && a[0][2]=='o'))
- {
- System.out.println(nameO + " is the winner");
- System.out.println();
- return false;
- }
- //executes if no possible winner
- else if(count==9)
- {
- //Needs amending
- System.out.println("##");
- System.out.println();
- System.out.println(nameX +" and "+ nameO + " tied.");
- System.out.println();
- return false;
- }
- //keeps the current game active
- else
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement