gpt4 book ai didi

java - 捡稻草游戏 : Helping the computer learn from its' mistakes?

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:40 24 4
gpt4 key购买 nike

所以我已经编写了游戏。你轮流与电脑对抗,挑选 1-3 根吸管,游戏的重点是留给对手 1 根吸管。整个部分都有效,但现在我必须对计算机进行编程,以便通过连续播放变得更聪明。

但是,我并不完全确定这是如何完成的。向我解释的方式是你有 4 个“杯子”,我认为它们是数组。每个杯子包含 Action (1, 2, 3)。在计算机回合期间,它随机挑选一个杯子并随机选择三个 Action 之一。如果该 Action 不起作用,则会将其从杯子中移除。

经过几场比赛后,计算机应该变得无与伦比。我将继续努力,但我遇到了很多麻烦。我已经研究了几个小时了,所以我只会发布原始代码,而不包含损坏的部分。

编辑:我在底部添加了 Cup 类。

import java.util.*;
public class Nim
{
public static void main(String[] args)
{
Random r = new Random();
Scanner kb = new Scanner(System.in);
String reply;

int straws, cupNum, prevCupNum, prevComputerMove,
computerMove, humanMove, gameNumber = 0, humanWin = 0;

// create an array of four cups
Cup[] cup = new Cup[4];
for (int i = 0; i < cup.length; i++)
cup[i] = new Cup();


System.out.println("Let's play Nim");
System.out.println();
do
{
gameNumber++;
straws = r.nextInt(11) + 10;
// add 1 if necessary so computer nevers starts in losing config
if (straws%4 == 1)
straws++;
System.out.println("Straws to start = " + straws);
System.out.println();

// set to -1 to indicate just starting so no previous move
prevCupNum = -1; // no prev move so init to -1
prevComputerMove = -1;

do
{
cupNum = straws%4; // get cup number

// if cup is empty, then use 1 for move
if (cup[cupNum].count() == 0) // cup empty then move = 1
computerMove = 1;
else
computerMove = cup[cupNum].select(); // get move from cup



/*MISSING CODE
If cup is cup number 1, then remove the previous move
from the previous cup unless already empty*/

System.out.println
("Computer picks up " + computerMove + " straws");
straws = straws - computerMove;
if (straws < 0)
straws = 0;
System.out.println(straws + " left");
System.out.println();

// save this move so it can be removed later if necessary
prevCupNum = cupNum;
prevComputerMove = computerMove;

if (straws == 0)
{
System.out.println("Wow. You win!");
humanWin++;

/* MISSING CODE
Remove last move computer made from cup*/
}
else // get move from human
{
System.out.println();
do
{
System.out.print("Your move: enter ");

if (straws == 1)
System.out.println("1");
else
if (straws == 2)
System.out.println("1 or 2");
else
if (straws >= 3)
System.out.println("1, 2, or 3");
humanMove = kb.nextInt();
} while
(humanMove > 3 || humanMove < 1 || straws - humanMove < 0);

straws = straws - humanMove;
System.out.println(straws + " left");
if (straws == 0)
{
System.out.println();
System.out.println("Ha, ha. You lose!");
}
}
} while (straws > 0);


System.out.println
("Score: Human " + humanWin + " Computer " +
(gameNumber - humanWin));

if (kb.hasNextLine()) // get rid of stray newline
kb.nextLine();
System.out.println();
System.out.println("Want to play another game? Hit enter.");
System.out.println
("Or are you too CHICKEN? In that case, type in quit.");
reply = kb.nextLine();
reply = reply.trim();
if (reply.length() > 4) // require only 1st 4 to be quit
reply = reply.substring(0, 4);

}
while (!reply.toLowerCase().equals("quit"));
}
public class Cup
{
ArrayList<Integer> c = new ArrayList<Integer>();

public Cup()
{
c.add(1);
c.add(2);
c.add(3);
}
//-----------------------
public int count()
{
return c.size();
}
//-----------------------
public int select()
{
// random is a static method in Math that returns a double
// value greater than or equal to 0.0 and less than 1.0.
int index = (int)(c.size()*Math.random());
return c.get(index);
}
//-----------------------
public void remove(Integer move)
{
c.remove(move);
}
}

最佳答案

恐怕我不明白“杯赛”策略。然而,有一种更简单的策略可以迫使机器通过玩耍来学习。当它播放时,记录它在轮到后留下的吸管数量。如果输了,就确保在接下来的比赛中不会轮流留下那么多稻草。

你的代码看起来像这样:

class AI_Player {
private final Set<Integer> losingMoves = new HashSet<>();
private final Set<Integer> currentGameMoves = new HashSet<>();

public void startGame() {
currentGameMoves.clear();
}

public int nextMove(int strawsRemaining) {
List<Integer> possibleMoves = Stream.of(1, 2, 3)
.filter(move -> move < strawsRemaining)
.filter(move -> !losingMoves.contains(strawsRemaining - move))
.collect(Collectors.toList());
int move = possibleMoves.get(Random.nextInt(possibleMoves.size()));
currentGameMoves.add(strawsRemaining - move);
return move;
}

public void registerLoss() {
losingMoves.addAll(currentGameMoves);
}

public void registerWin() {
losingMoves.removeAll(currentGameMoves);
}
}

这使用 Java 8 流,但如果您还没有使用过它们,转换起来相当简单。

关于java - 捡稻草游戏 : Helping the computer learn from its' mistakes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662668/

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