gpt4 book ai didi

java - java中的井字游戏,覆盖问题

转载 作者:行者123 更新时间:2023-12-01 09:59:58 25 4
gpt4 key购买 nike

这是一个简单的井字游戏,一切都是正确的,但有一个问题,任何人都可以覆盖以前的标记并放置他的

我想禁用覆盖,我该怎么做?

我们必须使用最终语法吗?

import java.util.*;
class TicTacToe {
static String Side;
static int NumberOfMoves=0;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Functions fc = new Functions();
int move;
System.out.println("Enter your Side :\n1) X\n2) O");
Side = sc.next();
if(Side.equals("1") ||Side.equals("x") ||Side.equals("X")) {
Side = "X";
} else if(Side.equals("2") ||Side.equals("o") ||Side.equals("O")) {
Side = "O";
}
fc.initialize();
fc.DisplayBoard();
while(fc.CheckIfWin() == 0) {
System.out.println("Its "+Side+"'s turn");
move = sc.nextInt();
fc.Move(move);
NumberOfMoves++;
fc.CheckIfWin();
fc.DisplayBoard();
fc.SideChange();
}
}
static class Functions {
String Row[] = new String[9];
public void initialize() {
for(int i = 1;i<=9;i++) {
Row[i-1] = Integer.toString(i);
}
}
public void DisplayBoard() {
System.out.print('\u000C');
System.out.println(" "+Row[0]+" | "+Row[1]+" | "+Row[2]);
System.out.println("---|---|---");
System.out.println(" "+Row[3]+" | "+Row[4]+" | "+Row[5]);
System.out.println("---|---|---");
System.out.println(" "+Row[6]+" | "+Row[7]+" | "+Row[8]);
}
public int CheckIfWin() {
/* Possible wins :-
* Horzontal : ( 0=1=2 ), (3, 4, 5), (6, 7, 8)
* Vertical : ( 0=3=6 ), (1=4=7), (2=5=8)
* Croswards : (0=4=8 ), (2=4=6)
*/
if(Row[0].equals(Row[1]) && Row[1].equals(Row[2])) {
System.out.println(Side+" wins.");
Row[0] = "--";Row[1] = "--";Row[2] = "--";
return 1;
} else if(Row[3].equals(Row[4]) && Row[4].equals(Row[5])) {
System.out.println(Side+" wins.");
Row[3] = "--";Row[4] = "--";Row[5] = "--";
return 1;
} else if(Row[6].equals(Row[7]) && Row[7].equals(Row[8])) {
System.out.println(Side+" wins.");
Row[6] = "--";Row[7] = "--";Row[8] = "--";
return 1;
} else if(Row[0].equals(Row[3]) && Row[3].equals(Row[6])) {
System.out.println(Side+" wins.");
Row[0] = "|";Row[3] = "|";Row[6] = "|";
return 1;
} else if(Row[1].equals(Row[4]) && Row[4].equals(Row[7])) {
System.out.println(Side+" wins.");
Row[1] = "|";Row[4] = "|";Row[7] = "|";
return 1;
} else if(Row[2].equals(Row[5]) && Row[5].equals(Row[8])) {
System.out.println(Side+" wins.");
Row[2] = "|";Row[5] = "|";Row[8] = "|";
return 1;
} else if(Row[0].equals(Row[4]) && Row[4].equals(Row[8])) {
System.out.println(Side+" wins.");
Row[0] = "\\";Row[4] = "\\";Row[8] = "\\";
return 1;
} else if(Row[2].equals(Row[4]) && Row[4].equals(Row[6])) {
System.out.println(Side+" wins.");
Row[2] = "/";Row[4] = "/";Row[6] = "/";
return 1;
}
return 0;
}
public void SideChange() {
if(Side.equals("O")) {
Side = "X";
} else {
Side = "O";
}
}
public void Move(int move) {
try {
Row[move-1] = Side;
} catch(Exception e) {
System.out.println("Tried to cheat\n Move will not be accepted");
}
}
}
}

最佳答案

这是非常学术性的,所以我只能给你一些建议,而不是解决方案。当您输入新的移动时,放置一个返回 boolean 值的方法(如 isValid(int move)),如果该区域被占用,则返回 false。然后将移动请求放在等待有效移动的时间

关于java - java中的井字游戏,覆盖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com