Bootstrap

tictactoe java 提高功能

棋盘最大99
人数最多26
可以保存游戏,加载之前的游戏

import java.util.Scanner;
import java.io.*;

public class TicTacToe {
// Set up the the game
public static final int empty = 0;
public static final int[] player = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
public static int playerNumber;
public static int winSeq;

// Different states
public static final int play = 0;
public static final int draw = 27;
public static final int[] playerWon = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

// The size of the game board
public static int rowNum, colNum; // number of rows and columns
public static int[][] board = new int[99][99]; // game board in 2D array

public static int currentState; // the current state of the game(play, draw, playerWon[])

public static int currentPlayer; // the current player (player[])
public static int currntRow, currentCol; // current turn's row and column

public static Scanner in = new Scanner(System.in); // the input Scanner

/** The entry main method */
public static void main(String[] args) {
// Initialize the game-board and current status
init();
print();
// Play the game
do {
move(currentPlayer); // player move
newState(currentPlayer); // update state
print();

// Print message if game over
if (currentState == draw) {
System.out.println("It's a Draw! Game Over!");
}
if (currentState>0&&currentState<27){
System.out.println("player "+currentState+" wins! Bye!");
}
if (currentPlayer==player[playerNumber-1]){
currentPlayer = player[0];
}
else{
currentPlayer = player[currentPlayer];
}
} while (currentState == play); // repeat if not
}

/*Initialize the game*/
public static void init() {
System.out.println("Do you want to resume a saved game? y/n");//y to resume, otherwise start a new game
String load = in.next();
if(load.equals("y")||load.equals("Y")){
System.out.println("Please enter your file name. *.txt");
String loadName = in.next();
loadGame(loadName);
}
//Initialize a new game
else{
boolean valid1 = false;// for input validation
do{
System.out.println("Please input the size of the board. 2-99");
int size = in.nextInt();
if(size>1&&size<100){
rowNum = size;
colNum = size;
valid1 = true;
}
else{
System.out.println("Input is not valid");
}
}while (!valid1);
boolean valid2 = false;
do{
System.out.println("Please input the number of player. 2-26");
int num = in.nextInt();
if(num>1&&num<27){
playerNumber = num;
valid2 = true;
}
else{
System.out.println("Input is not valid");
}
}while (!valid2);
System.out.println("Please input the win sequence count.");
winSeq = in.nextInt();
for (int row = 0; row < rowNum; ++row) {
for (int col = 0; col < colNum; ++col) {
board[row][col] = empty; // all cells empty
}
}
currentState = play; // ready to play
currentPlayer = player[0]; // cross plays first

//Check to ensure that winning is possible given the criteria specified.
if((winSeq-1)*playerNumber>rowNum*colNum-1){
System.out.println("Nobody can win under given criteria! Bye!");
System.exit(0);
}
}

}

// Player with the "turn" makes one move, or Q to save and quit.
// Update currentRow and currentCol.
public static void move(int turn) {
boolean validInput = false; // for input validation
do {
System.out.print("Player "+turn+", enter your move. Enter Q/q to save and quit");
String temp = in.next();
if(temp.equals("Q") || temp.equals("q")){
System.out.println("Please enter your save name. *.txt");
String saveName = in.next();
saveGame(saveName);
System.out.println("Game has saved. Bye!");
System.exit(0);
}
else{
int row = Integer.valueOf(temp) - 1; // array index starts at 0
int col = in.nextInt() - 1;
if (row >= 0 && row < rowNum && col >= 0 && col < colNum && board[row][col] == empty) {
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = turn; // update game board content
validInput = true; // input is valid, exit loop
}
else {
System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}

}
} while (!validInput); // repeat until input is valid
}

// Update the currentState after the player moved
public static void newState(int turn) {
if (win(turn)) { // check if winning move
currentState = playerWon[turn-1];
} else if (draw()) { // check for draw
currentState = draw;
}
// Otherwise, still playing.
}

// Return true if it is a draw
public static boolean draw() {
for (int row = 0; row < rowNum; ++row) {
for (int col = 0; col < colNum; ++col) {
if (board[row][col] == empty) {
return false; // an empty cell found, not draw, exit
}
}
}
return true; // it's a draw if no empty cell
}

//Return true if the player has won after placing at (currentRow, currentCol)
public static boolean win(int turn) {
int examine = 0;

//examine if there is a win in column
for(int i=0;i<colNum;i++){
examine = 0;
for(int j=0;j<rowNum-1;j++){
if (board[i][j] == turn && board[i][j+1] == turn){
examine++;
if(examine == winSeq - 1){
return true;
}
}
}
}

//examine if there is a win in row
for(int j=0;j<rowNum;j++){
examine = 0;
for(int i=0;i<colNum-1;i++){
if (board[i][j] == turn && board[i+1][j] == turn){
examine++;
if(examine == winSeq - 1){
return true;
}
}
}
}


//examine if there is a win from upper left to lower right
for(int i=0;i<colNum-1;i++){
examine = 0;
for(int j=0;j<rowNum-1;j++){
// if (board[i][j] == turn && board[i+1][j+1] == turn){
// examine++;
// if(examine == winSeq - 1){
// return true;
// }
// }
while(i<colNum-1&&j<rowNum-1&&board[i][j] == turn && board[i+1][j+1] == turn){
examine++;
if(examine == winSeq - 1){
return true;
}
i++;
j++;
}
}
}


//examine if there is a win from lower left to upper right
for(int i=colNum-1;i>0;i--){
examine = 0;
for(int j=0;j<rowNum-2;j++){
while(i>0&&j<rowNum-1&&board[i][j] == turn && board[i-1][j+1] == turn){
examine++;
if(examine == winSeq - 1){
return true;
}
i--;
j++;
}
}
}

return false;

}

/** Print the game board */
public static void print() {
System.out.print(" ");
for(int i=0;i<colNum;i++){
if(i<9){
System.out.print((i+1)+" ");
}
else{
System.out.print((i+1)+" ");
}
}
System.out.println();
for(int i=0;i<colNum;i++){
if(i<9){
System.out.print(" "+(i+1));
}
else{
System.out.print((i+1));
}
for(int j=0;j<rowNum;j++){
System.out.print(" ");
if (board[i][j]==0){
System.out.print(" ");
}
else if(board[i][j]==1){
System.out.print("X");
}
else if(board[i][j]==2){
System.out.print("O");
}
else if(board[i][j]<=16){
System.out.print((char)(62+board[i][j]));
}
else if(board[i][j]<=24){
System.out.print((char)(63+board[i][j]));//skip "O"
}
else{
System.out.print((char)(64+board[i][j]));//skip "O"
}
System.out.print(" ");
if(j!=rowNum-1){
System.out.print("|");
}
}
System.out.println();
if(i!=colNum-1){
System.out.print(" ---");
for(int j=0;j<rowNum-1;j++){
System.out.print("+---");
}
}
System.out.println();
}



}

/** Print a cell with the specified "content" */


//Save game in a txt file.
public static void saveGame(String fileName){
FileWriter writer;
try {
writer = new FileWriter(fileName);
writer.write(colNum+"\r\n");
writer.write(rowNum+"\r\n");
writer.write(playerNumber+"\r\n");
writer.write(winSeq+"\r\n");
for (int i=0;i<colNum;i++){
for (int j=0;j<rowNum;j++){
writer.write(board[i][j]+"\r\n");
}
}
writer.write(currentState+"\r\n");
writer.write(currentPlayer+"\r\n");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Load game from a txt file.
public static void loadGame(String fileName){
try {
Scanner in = new Scanner(new File(fileName));
colNum = Integer.valueOf(in.nextLine());
rowNum = Integer.valueOf(in.nextLine());
playerNumber = Integer.valueOf(in.nextLine());
winSeq = Integer.valueOf(in.nextLine());
for (int i=0;i<colNum;i++){
for (int j=0;j<rowNum;j++){
board[i][j] = Integer.valueOf(in.nextLine());
}
}
currentState = Integer.valueOf(in.nextLine());
currentPlayer = Integer.valueOf(in.nextLine());
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}
;