Closed. This question needs to be more focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
6年前关闭。
Improve this question
我现在完全用if语句来编写此东西,我觉得这是解决此任务的一种内在错误的方法。我可以得到每个玩家的第一个举动,但是我想不出一种方法来“保存”第一个玩家移动后棋盘的状态,它只是打印出玩家1的第一个举动的棋盘,然后打印玩家2的第一个棋盘。第一步,但它们不在同一块板上。我在这里感觉有点不合时宜...
想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
6年前关闭。
Improve this question
import java.util.Scanner;
public class gameBoard
{
public static void main(String[] args)
{
String str1;
Scanner scan = new Scanner(System.in);
System.out.println("Player 1 please enter 1 or 2, 1 = O, 2 = X");
int a = scan.nextInt();
if(a == 1){
String str2 = "O";
str1 = str2;
}else{
String str2 = "X";
str1 = str2;
}
System.out.println("Player 1 please enter the ROW (1, 2 or 3) you want: ");
int b = scan.nextInt();
if (b == 1 || b == 2 || b == 3){
System.out.println("Player 1 please enter the COLUMN you want: ");
int c = scan.nextInt();
if( c == 1 || c == 2 || c == 3){
if ( b == 2 && c == 2){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | " + str1 + " | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (b == 1 && c == 1){
System.out.println(str1 + " | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (b == 2 && c == 1){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(str1 + " | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (b == 3 && c == 1){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(str1 + " | | ");
}
if (b == 1 && c == 2){
System.out.println(" | " +str1 + " | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if( b == 3 && c == 2){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | " + str1 + "| ");
}
if (b == 1 && c == 3){
System.out.println(" | |" + str1);
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (b == 2 && c == 3){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | " + str1);
System.out.println("-----------");
System.out.println(" | | ");
}
if ( b == 3 && c == 3){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | " + str1);
}
System.out.println("Player 2 please enter 1 or 2, 1 = O, 2 = X");
int e = scan.nextInt();
if(e == 1){
String str2 = "O";
str1 = str2;
}else{
String str2 = "X";
str1 = str2;
}
System.out.println("Player 2 please enter the ROW (1, 2 or 3) you want: ");
int f = scan.nextInt();
if (f == 1 || f == 2 || f == 3){
System.out.println("Player 2 please enter the COLUMN you want: ");
int g = scan.nextInt();
if( g == 1 || g == 2 || g == 3){
if ( f == 2 && g == 2){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | " + str1 + " | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (f == 1 && g == 1){
System.out.println(str1 + " | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (f == 2 && g == 1){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(str1 + " | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (f == 3 && g == 1){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(str1 + " | | ");
}
if (f == 1 && g == 2){
System.out.println(" | " +str1 + " | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if( f == 3 && g == 2){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | " + str1 + "| ");
}
if (f == 1 && g == 3){
System.out.println(" | |" + str1);
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
}
if (f == 2 && g == 3){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | " + str1);
System.out.println("-----------");
System.out.println(" | | ");
}
if ( f == 3 && g == 3){
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | ");
System.out.println("-----------");
System.out.println(" | | " + str1);
}
System.out.println("");
}
}
}
}
}
}
我现在完全用if语句来编写此东西,我觉得这是解决此任务的一种内在错误的方法。我可以得到每个玩家的第一个举动,但是我想不出一种方法来“保存”第一个玩家移动后棋盘的状态,它只是打印出玩家1的第一个举动的棋盘,然后打印玩家2的第一个棋盘。第一步,但它们不在同一块板上。我在这里感觉有点不合时宜...
最佳答案
您应该更倾向于使用字段(用于“保存”)和方法(用于重复使用代码位)
试试这个例子:
import java.util.Scanner;
public class GameBoard {
// Use a matrix to emulate a 3*3 grid.
private String[][] board = new String[3][3];
private String[][] players = { { "Player 1", "X" }, { "Player 2", "O" } };
private int currentPlayer = -1;
public boolean isBoardFull() {
for (String[] row : board) {
for (String col : row) {
if (col == null) {
return false;
}
}
}
return true;
}
public boolean gameIsWon() {
// I shall leave this to your imagination ;)
return false;
}
public void printBoard() {
for (String[] row : board) {
for (String col : row) {
System.out.print("|" + (col == null ? " " : col) + "|");
}
System.out.println("\n---------");
}
}
public void play() {
// Try-with-resource (Java 7+)
try (Scanner scanner = new Scanner(System.in)) {
while (!isBoardFull()) {
currentPlayer = (currentPlayer + 1) % 2;
boolean valid = false;
// Loop until a certain player makes a valid move
while (!valid) {
System.out.print(players[currentPlayer][0] + ", choose your row: \n> ");
int row = scanner.nextInt() - 1;
System.out.print(players[currentPlayer][0] + ", you have chosen row " + (row + 1) + ". Choose your column: \n> ");
int col = scanner.nextInt() - 1;
if (board[row][col] == null) {
board[row][col] = players[currentPlayer][1];
printBoard();
if (gameIsWon()) {
System.out.println(players[currentPlayer][0] + " wins!");
return;
}
valid = true; // This will allow players to switch turns
} else {
System.out.println("This slot is taken, try again!");
}
}
}
System.out.println("Draw!");
}
}
public static void main(String[] args) {
new GameBoard().play();
}
}
关于java - 必须为2个玩家编写井字游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23274163/
10-13 03:35