我需要知道在一场比赛和十字架比赛中发现胜利动作的最佳方法.源代码没关系,我只需要一个例子或者我可以开始的东西.
我唯一能想到的就是使用循环并测试玩家每次移动的每个方向,以搜索例如连续五次.有更快更有效的方法吗?
我可以看到这个问题(或类似的问题)已被问过几次,我已经搜索了很多谷歌,所以我可以试着理解它,但我绝对被卡住了.
我的任务是使用递归函数,使用"goodness"变量来确定哪个是计算机可以做出的最佳移动,我甚至有一个文档可以帮助解决这个问题,但对于我的生活,我只是不喜欢理解它.
如果有人可能需要一些时间来帮助我或分解我真正需要做的事情,我会非常感激,我将链接到目前为止我的代码,但这是一项任务,所以指导比直接答案更可取.我已经看过MinMax解决方案了,这肯定在我的掌握之上,我对编程非常陌生(特别是在C#只有几个月的经验)所以就这么简单!
以下是我想要遵循的建议解决方案:
http://erwnerve.tripod.com/prog/recursion/tictctoe.htm
public partial class Form1 : Form
{
public static string[,] Board = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
public bool Winner = false;
public string WinState;
private void Reset()
{
WinState = "";
Winner = false;
Board[0, 0] = "1";
Board[0, 1] = "2";
Board[0, 2] = "3";
Board[1, 0] = "4";
Board[1, 1] = "5";
Board[1, 2] = "6";
Board[2, …
Run Code Online (Sandbox Code Playgroud) 我正在java中制作一个Tic Tac Toe程序,因为我正在学习java,我认为一个简单的项目将是一个很好的起点.到目前为止这是我的代码:
public class Start {
public static void main(String[] args) {
GameTicTacToe gameTicTacToe = new GameTicTacToe();
gameTicTacToe.windowBirth();
}
}
Run Code Online (Sandbox Code Playgroud)
和,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameTicTacToe implements ActionListener {
private int gridSize = 3;
private JButton[] gridButton = new JButton[(gridSize * gridSize)];
private JPanel grid = new JPanel(new GridLayout(gridSize, gridSize, 0, 0));
private JFrame windowTicTacToe = new JFrame("Tisk, Task, Toes");
private int[] gridButtonOwner = new int[(gridSize * gridSize)];
private int turn = 1;
private int …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试自学Minimax算法,并且我已尝试在tic tac toe中实现它.然而,我的算法中存在一个错误,我无法弄清楚导致它的原因.
以下是完整的源代码(抱歉,文字墙!):
public class TicTacToe {
private static boolean gameEnded = false;
private static boolean player = true;
private static Scanner in = new Scanner(System.in);
private static Board board = new Board();
public static void main(String[] args){
System.out.println(board);
while(!gameEnded){
Position position = null;
if(player){
position = makeMove();
board = new Board(board, position, PlayerSign.Cross);
}else{
board = findBestMove(board);
}
player = !player;
System.out.println(board);
evaluateGame();
}
}
private static Board findBestMove(Board board) {
ArrayList<Position> positions = board.getFreePositions();
Board bestChild = …
Run Code Online (Sandbox Code Playgroud) 我正在写一个tictactoe程序,但它不是你传统的tictactoe
首先,棋盘是4x4,获胜的方法是在连续,列或对角线上获得3个对手和1个对手.所以以下将通过第一列赢得"O":
O|_|X|_
O|X|_|_
O| |_|_
X|_|_|_
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现一个minimax算法,以便为程序提供一个无法打败的"硬"模式.
我的问题是我不能希望创建一个具有所有可能的游戏状态的树,因此我必须提出某种功能来评估我可以生成的游戏状态.
我想我的问题是,我怎么能想出这样的功能呢?
这是一个tic tac toe发电机.仅限计算机与计算机,与通常的播放器与计算机略有不同.我为此编写了大部分代码,但我遇到的问题有时是在我生成游戏时,整个电路板都会填满,并且会出现一行X和一行O并且会出现一个关系.有时会生成两行X或两行O,并且游戏不会在第一行连续3行后停止......任何见解?谢谢.
namespace TicTacToe
{
public partial class Form1 : Form
{
private Random rn = new Random();
const int SIZE = 9;
char[] cell = new char[SIZE];
char firstPlayer = ' ', secondPlayer = ' ';
private void button1_Click(object sender, EventArgs e)
{
//Clear the labels and starting values
for (int i = 0; i < SIZE; i++)
{
cell[i] = ' ';
}
label10.Text = "";
//Pick X or O to go first
switch (rn.Next(2))
{
case …
Run Code Online (Sandbox Code Playgroud) 我正在为C编写一个简单的Tic Tac Toe代码游戏.我已经完成了大部分代码,但我希望AI永远不会丢失.
我读过有关minimax算法的内容,但我不明白.如何使用此算法使计算机能够赢或赢但永不丢失?
在我的方法newminimax499我有一个minimax算法,利用memoization和alpha beta修剪.该方法适用于3x3游戏,但是当我玩4x4游戏时,我会为计算机选择奇怪的意外位置.他仍然没有输球,但他似乎没有赢得胜利.为了说明这里的问题,从3x3和4x4的2场比赛开始.首先是一个3x3游戏的场景,其中玩家是X并进行第一步:
这不错,实际上这是人们期望计算机做的事情.现在来看看4x4游戏中的场景.O再次是计算机而X开始:
正如你所看到的,计算机只是一个接一个地将Os放入一个系统的顺序中,只有当它有潜在的胜利时才打破该命令以阻止X. 这是非常防守的比赛,不像在3x3比赛中看到的那样.那么为什么3x3和4x4的方法表现不同?
这是代码:
//This method returns a 2 element int array containing the position of the best possible
//next move and the score it yields. Utilizes memoization and alpha beta
//pruning to achieve better performance.
public int[] newminimax499(int a, int b){
//int bestScore = (turn == 'O') ? +9 : -9; //X is minimizer, O is maximizer
int bestPos=-1;
int alpha= a;
int beta= b;
int currentScore;
//boardShow();
String stateString = "";
for (int i=0; i<state.length; i++) …
Run Code Online (Sandbox Code Playgroud) 我对机器学习领域非常感兴趣,最近我得到了一个关于未来几周项目的想法.
基本上我想创造一个可以击败Tic Tac Toe的每个人的AI.该算法必须可以针对每个n*n板尺寸进行扩展,甚至可以针对其他维度(例如,对于游戏的3D模拟).
此外,我不希望算法提前知道游戏的任何内容:它必须自己学习.所以没有硬编码的ifs,也没有监督学习.
我的想法是使用人工神经网络作为主算法本身,并通过使用遗传算法训练它.所以我必须只编写游戏规则,然后每个人都在与自己作斗争,应该从头开始学习.
这是一个很大的项目,我不是这个领域的专家,但我希望通过这样的目标,学习很多东西.
python artificial-intelligence neural-network tic-tac-toe genetic-algorithm
我正在尝试实现一个 Q 学习代理来学习在井字游戏中与随机代理对战的最佳策略。
我制定了一个我相信会奏效的计划。只有一个部分我无法理解。这是因为环境中有两个玩家。
现在,Q Learning 代理应该对当前状态s
采取行动,给定一些策略采取a
的行动,给定动作的连续状态s'
,以及从该连续状态收到的任何奖励,r
。
让我们把它放到一个元组中 (s, a, r, s')
现在通常一个代理会对它在给定动作中遇到的每个状态采取行动,并使用 Q 学习方程来更新前一个状态的值。
然而,由于 Tic Tac Toe 有两个玩家,我们可以将状态集分成两个。一组状态可以是学习代理开始采取行动的状态。另一组状态可以是对手开始行动的地方。
那么,我们是否需要将状态一分为二?或者学习代理是否需要更新游戏中访问的每个状态?
I feel as though it should probably be the latter, as this might affect updating Q Values for when the opponent wins the game.
Any help with this would be great, as there does not seem to be anything online that helps with my predicament.