我正在尝试为Java中的四连环游戏创建一个非常基本的AI响应。目前,我有一款功能游戏,两个人类玩家可以互相对抗。但是,我想包含极其基本的AI功能,以便人类可以在计算机上玩游戏。
例如,如果玩家1(人类)选择一列并播放计数器,那么玩家2(计算机)将在人类玩家轮到后立即将计数器自动放置在随机列中(无论是否只要计算机有响应,此举是好是坏。这可能吗?我一直在网上阅读有关minimax解决方案的信息,但是我不需要实现一些复杂的事情。
这是当两个人类玩家玩游戏时我当前的游戏类代码:
play.java
public class play {
private Connect4Game connect;
public play(Connect4Game connect) {
this.connect=connect;
}
public void playGame() {
System.out.println("Welcome to Connect 4");
System.out.println("To play the game type in the number of the column you want to drop you counter in");
System.out.println("Player One = r Player 2 = y");
System.out.println("");
board boardObj = new board(connect);
boardObj.printBoard();
boolean win = false;
while(!win){
// player 1
String userInput = getUserInput();
int move = Integer.parseInt(userInput);
counter counterObj = new counter(connect);
counterObj.placeCounter('r', move);
boolean hasWon = false;
int count = 0;
// check horizontal
for(int i=0; i<connect.board.length; i++){
for(int j=0; j<connect.board[i].length; j++){
if(connect.board[i][j] == 'r'){
count = count + 1;
if(count == 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<connect.board[0].length; i++){
for(int j=0; j<connect.board.length; j++){
if(connect.board[j][i] == 'r'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
boardObj.printBoard();
if(hasWon){
win = true;
System.out.println("You Have Won!!!");
}
else {
//player 2
userInput = getUserInput();
move = Integer.parseInt(userInput);
counterObj.placeCounter('y',move);
hasWon = false;
count = 0;
// check horizontal
for(int i=0; i<connect.board.length; i++){
for(int j=0; j<connect.board[i].length; j++){
if(connect.board[i][j] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<connect.board[0].length; i++){
for(int j=0; j<connect.board.length; j++){
if(connect.board[j][i] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
boardObj.printBoard();
if(hasWon){
win = true;
System.out.println("You Have Won!!!");
}
}
}
}
public String getUserInput(){
String toReturn = null;
try{
toReturn = connect.input.readLine();
}
catch(Exception e){
}
return toReturn;
}
更改我的代码的player 2部分的最佳方法是什么,以便计算机决定在何处放置柜台而不是人工放置?
答案 0 :(得分:2)
您应该使用播放器界面:
public interface Player {
public String getUserInput(Board board);
}
和2种实现方式,一种是人类的,另一种是机器人的:
public class HumanPlayer implements Player {
String name;
public HumanPlayer(String name) {
this.name=name;
}
public String getUserInput(Board board) {
// your code to ask a move from the user input (System.in.)
}
}
public class RobotPlayer implements Player {
String name;
public RobotPlayer(String name) {
this.name=name;
}
public String getUserInput(Board board) {
// random answer calculated by some AI logic
}
}
我看到您仍然没有将board
类重命名为Board
:)
在主应用程序中,您可以保留玩家列表:
List<Player> playersList;
playersList.add(new HumanPlayer("Ben"));
playersList.add(new RobotPlayer("R2D2"));
在主游戏循环中,通过选择下一个玩家来开始每次迭代:
int i=playerIndex=0;
while (!win) {
Player currentPlayer=players.get(i);
System.out.println("Current player:"+currentPlayer.name;
// put here all your code for one player
// ....
// calculate index of next player
i= i<playersList.size()?i++:0;
}
答案 1 :(得分:0)
只需使用Math.random()即可获取1到4之间的整数