Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tictactoe;
- /**
- *
- * @author DD
- */
- public class MyGame
- {
- private String [][] gameBoard = {{"1","|","2","|","3"},
- {"-","+","-","+","-"},{"4","|","5","|","6"},
- {"-","+","-","+","-"},{"7","|","8","|","9"}};
- private String playerGiven;
- private boolean victory = false;
- //-------- method used to retrieve victory condition ----------------------
- public boolean getVictory()
- {
- return victory;
- }// -------------------------- End of getter ----------------------------
- public void setGameBoard() {
- this.gameBoard = gameBoard;
- }
- public MyGame ()
- {
- for(int row=0;row<gameBoard.length;row++)
- {
- for(int col=0;col<gameBoard[row].length;col++)
- {
- System.out.print(gameBoard[row][col]);
- }
- System.out.println("");
- }
- System.out.println("");
- }// -------------- End of Constructor -------------------------
- public void displayGame(String playerGiven)
- {
- for(int row=0;row<gameBoard.length;row++)
- {
- for(int col=0;col<gameBoard[row].length;col++)
- {
- System.out.print(gameBoard[row][col]);
- }
- System.out.println("");
- }
- System.out.println("It's your turn Player "+ playerGiven);
- }// ------------------------ End of displayGame method ----------------------
- public void playingGame (String playerChoiceGiven, String playerGiven)
- {
- for(int row=0;row<5;row++)
- {
- for(int col=0;col<5;col++)
- {
- if(gameBoard[row][col].contains(playerChoiceGiven))
- {
- gameBoard[row][col]= playerGiven;
- }
- }
- }
- }// ---------------- End of playingGame method ------------------------------
- public void victoryCondition()
- {
- /* check from the the -individual columns-
- tried making it with for loops but the compiler starting going crazy.
- */
- if ((gameBoard[0][0]==gameBoard[2][0])&&
- (gameBoard[2][0] == gameBoard[4][0]))
- {
- System.out.println("Player: "+gameBoard[2][0]+ " WON!");
- victory = true;
- }
- if ((gameBoard[0][2]==gameBoard[2][2])&&
- (gameBoard[2][2] == gameBoard[4][2]))
- {
- System.out.println("Player: "+gameBoard[2][2]+ " WON!");
- victory = true;
- }
- if ((gameBoard[0][4]==gameBoard[2][4])&&
- (gameBoard[2][4] == gameBoard[4][4]))
- {
- System.out.println("Player: "+gameBoard[2][4]+ " WON!");
- victory = true;
- }
- /*
- check indivual rows are similar
- */
- if ((gameBoard[0][0]==gameBoard[0][2])&&
- (gameBoard[0][2] == gameBoard[0][4]))
- {
- System.out.println("Player: "+gameBoard[0][2]+ " WON!");
- victory = true;
- }
- if ((gameBoard[2][0]==gameBoard[2][2])&&
- (gameBoard[2][2] == gameBoard[2][4]))
- {
- System.out.println("Player: "+gameBoard[2][2]+ " WON!");
- victory = true;
- }
- if ((gameBoard[4][0]==gameBoard[4][2])&&
- (gameBoard[4][2] == gameBoard[4][4]))
- {
- System.out.println("Player: "+gameBoard[4][2]+ " WON!");
- victory = true;
- }
- /*
- check through the two diag patterns for winner.
- */
- if ((gameBoard[0][0]==gameBoard[2][2])&&
- (gameBoard[2][2] == gameBoard[4][4]))
- {
- System.out.println("Player: "+gameBoard[2][2]+ " WON!");
- victory = true;
- }
- if ((gameBoard[0][4]==gameBoard[2][2])&&
- (gameBoard[2][2] == gameBoard[4][0]))
- {
- System.out.println("Player: "+gameBoard[2][2]+ " WON!");
- victory = true;
- }
- // otherwise return false to continue game
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement