Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * A console version of tic-tac-toe.
- * KNOWN ISSUES:
- * -Instead of deciding who is X and who is O,
- * the logic decides whether X goes first or O goes first.
- * This issue was noticed too late to fix, so I left it. :P
- * Player 1 is always X and player 2 is always O, regardless
- * of who goes first.
- */
- public class TicTacToe {
- //class members
- static Scanner input = new Scanner(System.in);
- static String player1, player2;
- static char[][] board = new char[3][3];
- static boolean p1Plays;
- static boolean p1Wins, p2Wins;
- public static void main(String[] args) {
- int x, y;
- int guessP1, guessP2;
- //player1 setup
- System.out.print("X, enter your name: ");
- player1 = input.next();
- do {
- System.out.printf("%s, enter a number between 0 and 9: ", player1);
- guessP1 = input.nextInt();
- if(guessP1 > 9 || guessP1 < 0)
- System.out.println("Your guess is out of range!");
- } while(guessP1 > 9 || guessP1 < 0);
- System.out.println("");
- //player2 setup
- //they get checks and things to be sure they don't pick same as P1
- do {
- System.out.print("O, enter your name: ");
- player2 = input.next();
- if(player1.equals(player2))
- System.out.println("That name is taken.");
- } while(player1.equals(player2));
- do {
- System.out.printf("%s, enter a number between 0 and 9: ", player2);
- guessP2 = input.nextInt();
- if(guessP1 == guessP2)
- System.out.printf("%s already picked %d!\n", player1, guessP2);
- if(guessP2 > 9 || guessP2 < 0)
- System.out.println("Your guess is out of range!");
- } while(guessP1 == guessP2 || guessP2 > 9 || guessP2 < 0);
- System.out.println("");
- //decision time!
- p1Plays = decideIfP1GoesFirst(guessP1, guessP2);
- if(p1Plays)
- System.out.printf("%s goes first.", player1);
- else
- System.out.printf("%s goes first.", player2);
- //start game!
- for(y = 0; y < 3; y++) {
- for(x = 0; x < 3; x++)
- board[y][x] = ' ';
- }
- gameLoop();
- }
- //choose whether or not P1 goes first
- public static boolean decideIfP1GoesFirst(int guess1, int guess2) {
- int rando = (int)(Math.random() * 10.0);
- System.out.printf("Random number: %d\n", rando);
- return Math.abs(guess1 - rando) < Math.abs(guess2 - rando);
- }
- //print the game board
- public static void printBoard() {
- int x, y;
- System.out.printf("%s is X.\n%s is O.\n\n", player1, player2);
- System.out.println(" A B C");
- for(y = 0; y < 3; y++) {
- System.out.printf("%d ", y + 1);
- for(x = 0; x < 3; x++) {
- System.out.printf("%1c", board[y][x]);
- if(x != 2) System.out.print("|");
- }
- if(y != 2) System.out.print("\n -+-+-");
- System.out.println("");
- }
- }
- //the core game loop
- public static void gameLoop() {
- boolean isValidMove;
- String move;
- while(!p1Wins && !p2Wins) {
- //clear screen; this is the only way that seems to work
- System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
- //print board
- printBoard();
- //is the board full?
- if(boardIsFull()) {
- System.out.println("\nBoard is full!\nGame over.");
- break;
- }
- //ask for move
- isValidMove = false;
- do {
- //input prompt
- System.out.println();
- if(p1Plays)
- System.out.printf("%s, where are you going to play? (ex. A1) ", player1);
- else
- System.out.printf("%s, where are you going to play? (ex. A1) ", player2);
- move = input.next().toUpperCase();
- //set the board!
- if(setBoard(move)) isValidMove = true;
- } while(!isValidMove);
- //loop back, switch players
- p1Plays = !p1Plays;
- }
- }
- //test if the board is full
- public static boolean boardIsFull() {
- int y, x;
- for(y= 0; y < 3; y++) {
- for(x = 0; x < 3; x++){
- if(board[y][x] == ' ') return false;
- }
- }
- return true;
- }
- //make the move, but ONLY if it is valid
- public static boolean setBoard(String move) {
- int y, x;
- //this set of switches tests input integrity
- //check row
- switch(move.charAt(0)) {
- case 'A':
- x = 0;
- break;
- case 'B':
- x = 1;
- break;
- case 'C':
- x = 2;
- break;
- default:
- System.out.println("Input is invalid!");
- return false;
- }
- //check column
- switch(move.charAt(1)) {
- case '1':
- y = 0;
- break;
- case '2':
- y = 1;
- break;
- case '3':
- y = 2;
- break;
- default:
- System.out.println("Input is invalid!");
- return false;
- }
- //see if the space is taken
- if(board[y][x] != ' ') {
- System.out.println("This space is taken!");
- return false;
- }
- //place in board
- if(p1Plays) board[y][x] = 'X';
- else board[y][x] = 'O';
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement