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的第一个移动板,但他们不在同一块板上。我觉得这里的联赛有点不合适......
答案 0 :(得分:1)
您应该更倾向于使用字段(用于"保存")和方法(用于重复使用代码位)
试试这个例子:
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();
}
}
答案 1 :(得分:0)
我从你的新课程中创建了一个新课程,这个课程将保存用户输入的元素,只需完成它:
import java.util.Scanner;
public class gameBoard {
public static void main(String[] args) {
int n = 1, j, i, b;
char str1 = 'o';
Scanner scan = new Scanner(System.in);
char[][] g = new char[3][3];
for(i = 0; i < 3; i ++){
for(j = 0; j < 3; j ++){
if(j == 2){
System.out.print(n);
}
else{
System.out.print(n + " | ");
}
g[i][j] = ' ';
n++;
}
System.out.println("");
}
n = 1;
System.out.println("Player 1 please enter the square you want (1-9): ");
b = scan.nextInt();
while(b > 9 || b < 1){
System.out.println("Player 1 please enter the square you want (1-9): ");
b = scan.nextInt();
}
switch(b){
case 1:
if(g[0][0] == ' '){
g[0][0] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 2:
if(g[0][1] == ' '){
g[0][1] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 3:
if(g[0][2] == ' '){
g[0][2] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 4:
if(g[1][0] == ' '){
g[1][0] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 5:
if(g[1][1] == ' '){
g[1][1] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 6:
if(g[1][2] == ' '){
g[1][2] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 7:
if(g[2][0] == ' '){
g[2][0] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 8:
if(g[2][1] == ' '){
g[2][1] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
case 9:
if(g[2][2] == ' '){
g[2][2] = str1;
}
else{
System.out.println("Already played on that square");
}
break;
}
for(i = 0; i < 3; i ++){
for(j = 0; j < 3; j ++){
if(j == 2){
System.out.print(g[i][j]);
}
else{
System.out.print(g[i][j] + " | ");
}
n++;
}
System.out.println("");
}
System.out.print("Bye");
}
}
希望这有助于您了解如何完成它:)
祝你好运顺便说一下,你有一个Tic Tac Toe例子,你可以从那里引导自己。 here你有另一个。以防你被困在某事