Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Player {
- private String name; //each player's unique(?) name.
- private String piece; //either an x or an o -- x form p1, o for p2
- //constructor, takes in a name and player number.
- public Player(String name, int playNum) {
- this.name = name;
- if(playNum==1)
- piece="x";
- else
- piece="o";
- }
- //because you just never know.
- public String toString(){
- return name;
- }
- /*does a majority of the work for class player... prompts player for move position, checks for
- * invalid input (e.g. the letter k). then sends in that value to the moves[][] in class board.
- * checks the value in moves[][] and if it is full, restarts the turn. */
- public void makeMove() {
- Scanner scan = new Scanner(System.in);
- System.out.println("\n" + name + ", it is your turn.");
- int col=3, row=3;
- boolean failing = false;
- do {
- do { //continues to loop until a valid row number is given.
- failing=false;
- System.out.println("Which column would you like your piece to go in?");
- try {
- col = scan.nextInt();
- } catch (Exception e) { //should only work if any number that is not an integer
- System.out.println("That is not a valid column number.");
- failing = true;
- } finally {
- scan.nextLine();
- }
- if (col > 2 || col < 0) { //starts the loop over if the row given is illegal
- System.out.println("The only columns are 0, 1, and 2.");
- failing = true;
- }
- } while(failing);
- do { //this loop is the same as the row loop, but uses columns instead.
- failing=false;
- System.out.println("Which row would you like your piece to go in?");
- try {
- row = scan.nextInt();
- } catch (Exception e) {
- System.out.println("That is not a valid row number.");
- failing = true;
- } finally {
- scan.nextLine();
- }
- if (row > 2 || row < 0) {
- System.out.println("The only rows are 0, 1, and 2.");
- failing = true;
- }
- } while(failing);
- if(board.getMoves(col, row).trim().isEmpty()) {
- board.playerChoice(col, row, piece);
- failing=false;
- }
- else {
- failing=true;
- System.out.println("That space is already full. Please choose again.");
- }
- } while(failing);
- }
- }
- import java.util.Scanner;
- public class board { //-----------------------------------
- private static String[][] moves; //the array that holds the values and status of each place on the board
- private Player playerOne, playerTwo; //stores the name of each respective player
- private static int whoWon; //stores the three values below of either p1 winning, p2 winning, or a tie
- private static final int PLAYERONE = 1; //-----------------------------------
- private static final int PLAYERTWO = 2;
- private static final int ITS_A_TIE = 3;
- public static void main (String[] args) {
- board newGame = new board();
- newGame.game();
- }
- //game() calls mostly everything relevant to the operations of ttt.
- public void game() {
- System.out.println("Welcome to Tic-Tac-Toe!");
- whoWon = 0; //initializes it so that no one is winning
- Scanner scan = new Scanner(System.in);
- System.out.println("Player one, what is your name?");
- playerOne = new Player(scan.nextLine(), 1);
- System.out.println("Player two, what is your name?");
- playerTwo = new Player(scan.nextLine(), 2);
- moves = new String[3][3]; //initializes the board....
- for(int i = 0; i < moves.length; i++) {
- for(int j = 0; j <moves[i].length; j++) {
- moves[i][j] = " "; //..and fills it with spaces to keep formatting
- }
- }
- do{ //this loop cycles through each players' turn.
- display(); //display() shows the board.
- playerOne.makeMove(); //explained in Player class.
- detectGameOver(); //at the end of each players' turn, checks for a win or draw
- if(whoWon < 1) { //these lines only operate if the game did not end on p1's turn
- display();
- playerTwo.makeMove();
- detectGameOver();
- }
- } while(whoWon < 1); //whoWon is only above 0 if someone one. 1 represents p1 winning, 2 represents p2, and 3 represents a tie. ergo, when it as at 0, there is no win result
- display(); //shows the board one last time with the winning move.
- System.out.print("\nThe game is over, and the winner is ");
- if (whoWon == PLAYERONE) System.out.println(playerOne + ". Congratulations and good game!");
- else if (whoWon == PLAYERTWO) System.out.println(playerTwo + ". Congratulations and good game!");
- else if (whoWon == ITS_A_TIE) System.out.println("... no one. It appears to be a tie. Very drab. Oh well, nice try.");
- }
- //called from the player class to show their move on the board
- public static void playerChoice(int col, int row, String piece) {
- moves[col][row] = piece;
- }
- //shows the board each turn, separated by --- and # on cross-bars
- public void display() {
- for(int i = 0; i < moves.length; i++) {
- for(int j = 0; j <moves[i].length; j++) {
- if(j < 9)
- System.out.print(moves[i][j] + " | ");
- else
- System.out.print(moves[i][j]);
- }
- if(i < 9)
- System.out.println("\n--#---#---#---#---#---#---#---#---#---");
- }
- }
- //called from player
- public static String getMoves(int col, int row) {
- return moves[col][row];
- }
- //detects the barf that is the logic. Each of the || conditions represent a possible win.
- public static void detectGameOver() {
- if((moves[0][0].equals("o") && moves[0][1].equals("o") && moves[0][2].equals("o"))
- || (moves[0][0].equals("o") && moves[1][1].equals("o") && moves[2][2].equals("o"))
- || (moves[0][0].equals("o") && moves[1][0].equals("o") && moves[2][0].equals("o"))
- || (moves[0][2].equals("o") && moves[1][1].equals("o") && moves[2][0].equals("o"))
- || (moves[2][0].equals("o") && moves[2][1].equals("o") && moves[2][2].equals("o"))
- || (moves[0][1].equals("o") && moves[1][1].equals("o") && moves[2][1].equals("o"))
- || (moves[1][0].equals("o") && moves[1][1].equals("o") && moves[1][2].equals("o"))
- || (moves[0][2].equals("o") && moves[1][2].equals("o") && moves[2][2].equals("o"))
- ){
- whoWon = PLAYERTWO;
- }
- else if(
- (moves[0][0].equals("x") && moves[0][1].equals("x") && moves[0][2].equals("x"))
- || (moves[0][0].equals("x") && moves[1][1].equals("x") && moves[2][2].equals("x"))
- || (moves[0][0].equals("x") && moves[1][0].equals("x") && moves[2][0].equals("x"))
- || (moves[0][2].equals("x") && moves[1][1].equals("x") && moves[2][0].equals("x"))
- || (moves[2][0].equals("x") && moves[2][1].equals("x") && moves[2][2].equals("x"))
- || (moves[0][1].equals("x") && moves[1][1].equals("x") && moves[2][1].equals("x"))
- || (moves[1][0].equals("x") && moves[1][1].equals("x") && moves[1][2].equals("x"))
- || (moves[0][2].equals("x") && moves[1][2].equals("x") && moves[2][2].equals("x"))
- ){
- whoWon = PLAYERONE;
- }
- //represents a tie-- checks if every position is full but isn't a space (which is the initialized value
- else if(
- !moves[0][0].equals(" ") && !moves[0][1].equals(" ") && !moves[0][2].equals(" ")
- && !moves[0][0].equals(" ") && !moves[1][1].equals(" ") && !moves[2][2].equals(" ")
- && !moves[0][0].equals(" ") && !moves[1][0].equals(" ") && !moves[2][0].equals(" ")
- && !moves[0][2].equals(" ") && !moves[1][1].equals(" ") && !moves[2][0].equals(" ")
- && !moves[2][0].equals(" ") && !moves[2][1].equals(" ") && !moves[2][2].equals(" ")
- && !moves[0][1].equals(" ") && !moves[1][1].equals(" ") && !moves[2][1].equals(" ")
- && !moves[1][0].equals(" ") && !moves[1][1].equals(" ") && !moves[1][2].equals(" ")
- && !moves[0][2].equals(" ") && !moves[1][2].equals(" ") && !moves[2][2].equals(" ")
- ) {
- whoWon = ITS_A_TIE;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement