Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 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.        char[][] GameBoardInput = new char[3][3];
  39.        
  40.        int TempIntRow = 0;
  41.        int TempIntCol = 0;
  42.        
  43.        int X_position = 60;
  44.        int Y_position = 60;
  45.        
  46.        int PlayerTwoInput =0;
  47.        
  48.        final int OVAL_SIZE = 61;
  49.        
  50.             GameBoardInput[0][0]='-';//initializes the board with the characters 1-9
  51.             GameBoardInput[0][1]='-';
  52.             GameBoardInput[0][2]='-';
  53.             GameBoardInput[1][0]='-';
  54.             GameBoardInput[1][1]='-';
  55.             GameBoardInput[1][2]='-';
  56.             GameBoardInput[2][0]='-';
  57.             GameBoardInput[2][1]='-';
  58.             GameBoardInput[2][2]='-';
  59.        
  60.        //Simple way to preform the same action?
  61.        //Perhaps a clever for statement?
  62.        //Error checking required?
  63.        
  64.        
  65.        for (int PlayerOneInput=0; PlayerOneInput <= 4; PlayerOneInput++)
  66.            {
  67.                //Player One
  68.                displayBoard(GameBoardInput);
  69.                do
  70.               {
  71.                
  72.                System.out.println("Round: " + ((PlayerOneInput) +1) );
  73.                System.out.println("Player One 'X': enter row (1-2-3):");
  74.                if ((keyboard.nextInt()) < 1 || (keyboard.nextInt()) > 3)
  75.                {
  76.                    TempIntRow = (invalidInput(TempIntRow) -1);
  77.                }
  78.                    else
  79.                    TempIntRow = (keyboard.nextInt() -1);
  80.                    
  81.                System.out.println("Player One: enter column (1-2-3):");
  82.                if ((keyboard.nextInt()) < 1 || (keyboard.nextInt()) > 3)
  83.                {
  84.                    TempIntCol = (invalidInput(TempIntRow) -1);
  85.                }
  86.                     else    
  87.                     TempIntCol = (keyboard.nextInt() -1);
  88.                
  89.                
  90.               } while (checkValidInput(GameBoardInput, TempIntRow, TempIntCol) == false);
  91.              
  92.                //Check if placement is valid using GameBoradInput[][]
  93.                //If valid proceed with placement
  94.                //Else ask user to re-input
  95.                //Divide users into different methods?
  96.                
  97.                Player1[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
  98.                Player1[TempIntRow][TempIntCol].setBackground(Color.blue);
  99.                win.add((Player1[TempIntRow][TempIntCol]), 0);
  100.                win.repaint();
  101.                
  102.                //Player Two
  103.                if (PlayerTwoInput <= 3)
  104.             {
  105.                do
  106.                {
  107.                System.out.println("Player Two '0': enter row (1-2-3):");
  108.                TempIntRow = keyboard.nextInt();
  109.                System.out.println("Player Two: enter column (1-2-3):");
  110.                TempIntCol = keyboard.nextInt();
  111.                } while (checkValidInput(GameBoardInput, TempIntRow, TempIntCol) == false);
  112.                
  113.                //GameBoard
  114.                
  115.                Player2[TempIntRow][TempIntCol] = new Oval ((TempIntRow * X_position), (TempIntCol * Y_position), OVAL_SIZE, OVAL_SIZE);
  116.                Player2[TempIntRow][TempIntCol].setBackground(Color.white);
  117.  
  118.                win.add((Player2[TempIntRow][TempIntCol]), 0);
  119.                win.repaint();
  120.                PlayerTwoInput ++;
  121.             }
  122.                else
  123.                win.repaint();
  124.             }    
  125.            
  126.            //Call Check for Winner
  127.            //Pass gameboard array to method as argument
  128.            
  129.            System.out.print("Thankyou for playing :)");
  130.            keyboard.close();
  131.        }
  132.    
  133.        public int invalidInput(int TempInt)
  134.        {
  135.        do
  136.        {
  137.            System.out.println("INVALID INPUT: Please try again");
  138.            TempInt = keyboard.nextInt();    
  139.        }
  140.        
  141.        while ((TempInt < 1 || TempInt > 3));
  142.        return TempInt;
  143.            
  144.            
  145.     }
  146.        
  147.    public static boolean checkValidInput(char[][] TestArray,int TempIntRow, int TempIntCol)
  148.    {
  149.        if (TestArray[TempIntRow][TempIntCol] == '-')
  150.        return true;
  151.      else
  152.      System.out.println("INVALID INPUT!: please try again :) ");
  153.         return false;
  154.      
  155.    }
  156.        
  157.    public static void displayBoard(char[][] a)
  158.    {
  159.     //Fixed formatting to display in console properly
  160.     System.out.println(a[0][0]+"   "+a[0][1]+"   "+a[0][2]+"\n"+
  161.                         "_   _   _  \n\n"+
  162.                         a[1][0]+"   "+a[1][1]+"   "+a[1][2]+"\n"+
  163.                         "_   _   _  \n\n"+
  164.                         a[2][0]+"   "+a[2][1]+"   "+a[2][2]+"\n");
  165.  
  166.     }    
  167.        
  168.        public static boolean checkForWinner(char[][] a, String nameX,
  169.                                      String nameO, int count)
  170.    {  
  171.     //executes if "x" wins
  172.     if((a[0][0]=='x' && a[0][1]=='x' && a[0][2]=='x') ||
  173.         (a[1][0]=='x' && a[1][1]=='x' && a[1][2]=='x') ||
  174.         (a[2][0]=='x' && a[2][1]=='x' && a[2][2]=='x') ||
  175.         (a[0][0]=='x' && a[1][0]=='x' && a[2][0]=='x') ||
  176.         (a[0][1]=='x' && a[1][1]=='x' && a[2][1]=='x') ||
  177.         (a[0][2]=='x' && a[1][2]=='x' && a[2][2]=='x') ||
  178.         (a[0][0]=='x' && a[1][1]=='x' && a[2][2]=='x') ||
  179.         (a[2][0]=='x' && a[1][1]=='x' && a[0][2]=='x'))
  180.     {
  181.         System.out.println(nameX + " is the winner");
  182.         System.out.println();
  183.         return false;
  184.     }
  185.     //executes if "o" wins
  186.     else if((a[0][0]=='o' && a[0][1]=='o' && a[0][2]=='o') ||
  187.             (a[1][0]=='o' && a[1][1]=='o' && a[1][2]=='o') ||
  188.             (a[2][0]=='o' && a[2][1]=='o' && a[2][2]=='o') ||
  189.             (a[0][0]=='o' && a[1][0]=='o' && a[2][0]=='o') ||
  190.             (a[0][1]=='o' && a[1][1]=='o' && a[2][1]=='o') ||
  191.             (a[0][2]=='o' && a[1][2]=='o' && a[2][2]=='o') ||
  192.             (a[0][0]=='o' && a[1][1]=='o' && a[2][2]=='o') ||
  193.             (a[2][0]=='o' && a[1][1]=='o' && a[0][2]=='o'))
  194.     {
  195.         System.out.println(nameO + " is the winner");
  196.         System.out.println();
  197.         return false;
  198.     }
  199.     //executes if no possible winner
  200.     else if(count==9)
  201.     {
  202.         //Needs amending
  203.         System.out.println("##");
  204.         System.out.println();
  205.         System.out.println(nameX +" and "+ nameO + " tied.");
  206.         System.out.println();
  207.         return false;
  208.     }
  209.     //keeps the current game active
  210.     else
  211.         return true;
  212.    }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement