Public Static Final Scanner Console

1103 Words5 Pages
//Name:Xiao Yang //Email:xiaoyang@brandeis.edu //Date:20 Nov 2016 //PA6 p4 //Bugs: none import java.util.*; import java.util.Arrays; public class Problem4 { public static final Scanner console = new Scanner (System.in); public static final Random rand = new Random (); public static void main(String[] args) { //declare board char [][] table = new char [3][3]; char human = 'X '; char cpu = 'O '; //coordinate of moves: row and column int r; int c; System.out.println("Let 's play tic-tac-toe game."); String playOrNot = "yes"; // loop for repeating game while (playOrNot.startsWith ("y")) { //initialize board and display for (int i = 0; i < 3; i++) { for (int p = 0; p < 3; p++) { table[i][p] = ' '; } } showTable (table); //choose whether to go first System.out.println("Do you want to go first? yes or no"); String goFirst = console.next(); if (goFirst.startsWith("n")) { table[1][1] = 'O '; showTable(table); } boolean end = false; //loop for asking moves while (!end) { //ask for user 's moves System.out.println("Please choose the row of your move (1~3)"); r = console.nextInt() - 1; System.out.println("Please choose the column of your move (1~3)"); c = console.nextInt() - 1; //store the move and display table [r][c] = 'X ';
Open Document