TicTacToe井字棋 by reinforcement learning

    对于初学强化学习的同学,数学公式也看不太懂, 一定希望有一些简单明了的代码实现加强对入门强化学习的直觉认识,这是一篇初级入门代码, 希望能对你们开始学习强化学习起到基本的作用.

井字棋具体玩法参考百度,这篇文字主要是用强化学习中Q-learning实现游戏过程中各个状态Q(S,a)计算, 在代码最后是一个人机对弈的过程, 电脑走第一步.

在Q-learning中, 各个Q(S,a)的计算公式如下:

     Q(S,a) = Q(S,a) + 0.1 * (reward(s,a) + 0.9 * Q(S',a') - Q(S,a))

reward(s,a)在电脑赢时返回1, 电脑输时返回-1, 其他时候返回0

在 计算完Q(S,a)后, 有一个人机对战代码,board = [0, 0, 0, 0, 0, 0, 0, 0, 0],代表棋盘初始状态, 电脑会选择最大Q(s,a)走第一步, 假如电脑选择的是位置3,那棋盘就成为board = [0, 0, 0, 1, 0, 0, 0, 0, 0],玩家也就是你随机选择一个位置,假如是位置0,在键盘输入0,棋盘变为board = [2, 0, 0, 1, 0, 0, 0, 0, 0],继续下去,直到结束状态。

我从没赢过电脑

代码位置:https://github.com/k13795263/TicTacToe/blob/master/TicTacToe.py

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

“more support, more energy, more useful code”

赞助魔方

<script type="text/javascript" src="//ri.revolvermaps.com/0/0/6.js?i=8kcekaxofo2&amp;m=7&amp;s=320&amp;c=e63100&amp;cr1=ffffff&amp;f=arial&amp;l=0&amp;bv=90&amp;lx=-420&amp;ly=420&amp;hi=20&amp;he=7&amp;hc=a8ddff&amp;rs=80" async="async"></script>

 

转载于:https://www.cnblogs.com/heidouInBlueSky/p/5742326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的井字棋 Java 代码示例: ```java import java.util.Scanner; public class TicTacToe { private char[][] board; // 棋盘 private char currentPlayer; // 当前玩家 public TicTacToe() { board = new char[3][3]; currentPlayer = 'X'; initializeBoard(); } // 初始化棋盘 public void initializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } } // 打印棋盘 public void printBoard() { System.out.println("-------------"); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + " | "); } System.out.println(); System.out.println("-------------"); } } // 切换玩家 public void changePlayer() { currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } // 检查是否已经有人胜利 public boolean checkForWin() { return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin()); } // 检查行是否有胜利者 public boolean checkRowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[i][0], board[i][1], board[i][2])) { return true; } } return false; } // 检查列是否有胜利者 public boolean checkColumnsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[0][i], board[1][i], board[2][i])) { return true; } } return false; } // 检查对角线是否有胜利者 public boolean checkDiagonalsForWin() { return (checkRowCol(board[0][0], board[1][1], board[2][2]) || checkRowCol(board[0][2], board[1][1], board[2][0])); } // 检查行或列是否有连续三个相同的字符 public boolean checkRowCol(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } // 下棋 public void placeMark(int row, int col) { if ((row >= 0) && (row < 3) && (col >= 0) && (col < 3)) { if (board[row][col] == '-') { board[row][col] = currentPlayer; } } } // 运行游戏 public void play() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; System.out.println("Let's play Tic Tac Toe!"); while (!gameOver) { System.out.println("Player " + currentPlayer + ", enter a row (0, 1, or 2): "); int row = scanner.nextInt(); System.out.println("Player " + currentPlayer + ", enter a column (0, 1, or 2): "); int col = scanner.nextInt(); placeMark(row, col); printBoard(); if (checkForWin()) { System.out.println("Congratulations! Player " + currentPlayer + " wins!"); gameOver = true; } else if (boardFull()) { System.out.println("Game over! It's a tie!"); gameOver = true; } else { changePlayer(); } } } // 检查棋盘是否已满 public boolean boardFull() { boolean full = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == '-') { full = false; } } } return full; } public static void main(String[] args) { TicTacToe game = new TicTacToe(); game.play(); } } ``` 这个示例中,我们创建了一个 `TicTacToe` 类,表示一个井字棋游戏。在 `TicTacToe` 类中,我们定义了一些方法,如 `initializeBoard` 方法用于初始化棋盘,`printBoard` 方法用于打印棋盘,`changePlayer` 方法用于切换玩家,`checkForWin` 方法用于检查是否已经有人胜利等等。我们还在 `main` 方法中创建了一个 `TicTacToe` 对象并调用了 `play` 方法来运行游戏。 在游戏运行过程中,我们使用 `Scanner` 类来读取玩家的输入。玩家在每轮中输入一个行号和列号,程序会根据这个输入来下棋。程序会在每轮结束时检查是否有人胜利或棋盘已满,如果有人胜利或棋盘已满,游戏结束。如果没有人胜利或棋盘未满,程序会切换到下一个玩家并进行下一轮游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值