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("");
}
}
}
}
}
}
I'm writing this thing entirely with if statements right now and I feel like this is an inherently wrong approach to this task. I can get the first move of each player but I cannot figure out a way to "save" the state the board is in after the first player moves, its just prints a board with player 1's first move, then prints a board with player 2's first move, but they are not on the same board together. I feel a bit out of my league here...
我现在用if语句写这个东西,我觉得这是一个固有的错误方法。我可以每个球员的第一步但是我不能找到一个方法来“拯救”状态董事会后第一个球员动作,只是输出一个与玩家1的第一步,然后输出一个与玩家2的第一步,但他们并不在同一个板在一起。我觉得在这里有点不合群……
2 个解决方案
#1
1
You should be more inclined to use fields (for "saving") and methods (for re-using bits of code)
您应该更倾向于使用字段(用于“保存”)和方法(用于重用代码位)
Try this example:
试试这个例子:
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();
}
}
#2
0
I made a new class from yours, this one will save elements from user input, just complete it:
我创建了一个新的类,它将从用户输入中保存元素,完成它:
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");
}
}
Hope this helps you giving an idea on how to finish it :)
希望这能帮助你对如何完成它有一个想法。
Good luck
祝你好运
Btw here you have a Tic Tac Toe example, you can guide yourself from there. And here you have another one. Just in case you're stuck in something
顺便说一句,你有一个Tic Tac脚趾的例子,你可以从那里指导自己。这里还有一个。以防你陷入困境
#1
1
You should be more inclined to use fields (for "saving") and methods (for re-using bits of code)
您应该更倾向于使用字段(用于“保存”)和方法(用于重用代码位)
Try this example:
试试这个例子:
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();
}
}
#2
0
I made a new class from yours, this one will save elements from user input, just complete it:
我创建了一个新的类,它将从用户输入中保存元素,完成它:
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");
}
}
Hope this helps you giving an idea on how to finish it :)
希望这能帮助你对如何完成它有一个想法。
Good luck
祝你好运
Btw here you have a Tic Tac Toe example, you can guide yourself from there. And here you have another one. Just in case you're stuck in something
顺便说一句,你有一个Tic Tac脚趾的例子,你可以从那里指导自己。这里还有一个。以防你陷入困境