[英]How do I determine a winner in my Tic Tac Toe program
先前的文章: 我如何使我的tictactoe程序可擴展
我試圖使Tic Tac Toe程序(人與計算機)具有可擴展性(可以更改板的大小)。 我之前遇到過重大問題,但大多數問題都已解決。
游戲的規則是井字游戲的基本規則,但另一個規則是,無論棋盤有多大( >= 5
),玩家或計算機連續只需要贏得5個得分即可。
現在,我程序的唯一突破性問題是確定誰贏得了比賽。 游戲目前可能只結束“平局”。 (而且我還沒有實現“ >= 5
”)。
具體的問題解釋是,我需要確定諸如“ computer wins
”和/或“ player wins
”之類的獲勝者和最終屏幕。
package tictactoe;
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static int size;
public static char[][] board;
public static int score = 0;
public static Scanner scan = new Scanner(System.in);
/**
* Creates base for the game.
*
* @param args the command line parameters. Not used.
*/
public static void main(String[] args) {
System.out.println("Select board size");
System.out.print("[int]: ");
size = Integer.parseInt(scan.nextLine());
board = new char[size][size];
setupBoard();
int i = 1;
while (true) {
if (i % 2 == 1) {
displayBoard();
getMove();
} else {
computerTurn();
}
// isWon()
if (isDraw()) {
System.err.println("Draw!");
break;
}
i++;
}
}
/**
* Checks for draws.
*
* @return if this game is a draw
*/
public static boolean isDraw() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
/**
* Displays the board.
*
*
*/
public static void displayBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("[%s]", board[i][j]);
}
System.out.println();
}
}
/**
* Displays the board.
*
*
*/
public static void setupBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = ' ';
}
}
}
/*
* Checks if the move is allowed.
*
*
*/
public static void getMove() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.printf("ROW: [0-%d]: ", size - 1);
int x = Integer.parseInt(sc.nextLine());
System.out.printf("COL: [0-%d]: ", size - 1);
int y = Integer.parseInt(sc.nextLine());
if (isValidPlay(x, y)) {
board[x][y] = 'X';
break;
}
}
}
/*
* Randomizes computer's turn - where it inputs the mark 'O'.
*
*
*/
public static void computerTurn() {
Random rgen = new Random(); // Random number generator
while (true) {
int x = (int) (Math.random() * size);
int y = (int) (Math.random() * size);
if (isValidPlay(x, y)) {
board[x][y] = 'O';
break;
}
}
}
/**
* Checks if the move is possible.
*
* @param inX
* @param inY
* @return
*/
public static boolean isValidPlay(int inX, int inY) {
// Play is out of bounds and thus not valid.
if ((inX >= size) || (inY >= size)) {
return false;
}
// Checks if a play have already been made at the location,
// and the location is thus invalid.
return (board[inX][inY] == ' ');
}
}
您已經具有循環播放的功能,因此,在每次迭代中,都以相同的方式檢查游戲isDraw()
,還檢查某些玩家是否贏了:
while (true) {
if (i % 2 == 1) {
displayBoard();
getMove();
} else {
computerTurn();
}
// isWon()
if (isDraw()) {
System.err.println("Draw!");
break;
} else if (playerHasWon()){
System.err.println("YOU WIN!");
break;
} else if (computerHasWon()) {
System.err.println("Computer WINS!\nYOU LOOSE!!");
break;
}
i++;
}
創建所需的方法后:
public static boolean playerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon ;
}
public static boolean computerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon ;
}
當然,下一個問題是如何創建這種方法? dunno如果您對此有疑問,但是在 這里和這里快速進行檢查,您會發現一些想法。
添加在:
為了澄清,我將使一個函數返回一個int
而不是booleans
,以使用某些常量檢查游戲是否完成:
private final int DRAW = 0;
private final int COMPUTER = 1;
private final int PLAYER = 2;
private int isGameFinished() {
if (isDraw()) return DRAW;
else if (computerHasWon()) return COMPUTER;
else if (playerHasWon()) return PLAYER;
}
然后只需檢查一個開關盒(在這里檢查如何在現場休息一會兒即可)
loop: while (true) {
// other stufff
switch (isGameFinished()) {
case PLAYER:
System.err.println("YOU WIN!");
break loop;
case COMPUTER:
System.err.println("Computer WINS!\nYOU LOOSE!!");
break loop;
case DRW:
System.err.println("IT'S A DRAW");
break loop;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.