当前位置:主页 > 资料 >

Creating a Rock Paper Scissors Game in Java with a Markov Ch
栏目分类:资料   发布日期:2018-08-03   浏览次数:

导读:本文为去找网小编(www.7zhao.net)为您推荐的Creating a Rock Paper Scissors Game in Java with a Markov Chain for the AI,希望对您有所帮助,谢谢! As Sheldon Cooper explains, the rules of RPSLS are the following: “Sci

本文为去找网小编(www.7zhao.net)为您推荐的Creating a Rock Paper Scissors Game in Java with a Markov Chain for the AI,希望对您有所帮助,谢谢!

欢迎访问www.7zhao.net



As Sheldon Cooper explains, the rules of RPSLS are the following : “Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors.”

本文来自去找www.7zhao.net

In that tutorial, you are going to create your own Rock Paper Scissors Lizard Spock Game in Java with an Artificial Intelligence based on a Markov Chain. 欢迎访问www.7zhao.net

Note that you can also watch this tutorial in video on YouTube : 内容来自www.7zhao.net

Representing Items of the Game

First step is to represent items for the Rock Paper Scissors Lizard Spock (RPSLS) Game. For that, we use an enum with five elements : 欢迎访问www.7zhao.net

  • ROCK
  • PAPER
  • SCISSORS
  • LIZARD
  • SPOCK

Each item will have a list of items against whom they lose. Besides, we will use a static block inside en enum declaration to initialize these lists. It gives us the following code for Item enum : copyright www.7zhao.net

Using a Markov Chain for the AI of the Game

For the Artificial Intelligence (AI) of our RPSLS Game, we are going to use a Markov Chain. It will let us to predict the next move that should be played by the user. Like that, we could react consequently and choosing an item against which the predicted item will lose. 去找(www.7zhao.net欢迎您

Like said by Wikipedia, A is “a describing a of possible events in which the probability of each event depends only on the state attained in the previous event”. www.7zhao.net

A diagram representing a two-state Markov process, with the states labelled E and A

In the above figure, you can see a diagram representing a two-state Markov process, with the states labelled E and A. Each number represents the probability of the Markov process changing from one state to another state, with the direction indicated by the arrow. www.7zhao.net

For example, if the Markov process is in state A, then the probability it changes to state E is 0.4, while the probability it remains in state A is 0.6.

欢迎访问www.7zhao.net

In our RPSLS Game, each state will be represented by an Item. During the game, we will store in a Matrix the number of events passing from an item to an other item. Thus, if the user has played ROCK at the previous throw and then, he plays PAPER, we will increment the ROCK to PAPER counter in the matrix. 去找(www.7zhao.net欢迎您

Our Matrix will have the following appearance after several throws :

www.7zhao.net

Choosing the next move to play

To choose the next move to play for our AI, we will use the Matrix presented previously. We need to know the previous throw made by the user. With that, we choose in the matrix the corresponding line. We iterate on the elements of that line and we choose the item for which the column has the maximum number of events. This is the predicted item for next move of the user.

本文来自去找www.7zhao.net

In our example, imagine the user has thrown a PAPER in the previous throw. By iterating on the associated line, we can predict that the next throw of the user will be SPOCK. 欢迎访问www.7zhao.net

So, we can randomly choose in the list of items against whom SPOCK item loses. It gives us the following code for the nextMove method : 欢迎访问www.7zhao.net

Creating the core of our RPSLS Game

Now, we can create the core of our RPSLS Game. First, we initialize the matrix for our Markov Chain. Then, we use a Scanner to get the user input. We update our matrix by calling the updateMarkovChain method.

去找(www.7zhao.net欢迎您

In the same time, we call the nextMove method to get the throw choosen by the computer. We check who is the winner of this throwing between user and computer. We display the result of this throwing on the console.

copyright www.7zhao.net

When the user enters STOP, the game is ended. We display some statistics to check the level of our AI for the RPSLS Game. It gives us the following complete code for our game : 内容来自www.7zhao.net

Rock Paper Scissors Lizard Spock Game in Action

Now, it’s time to put our RPSLS Game in Action. Once the game is launched, we can play it in the console. After some throws, we can see our AI based on a Markov Chain seems really great :

本文来自去找www.7zhao.net

Indeed, the statistics after 22 throws are the following : 内容来自www.7zhao.net

  • User win : 5/22 (22.7%)
  • Tie : 3/22 (13.6%)
  • Computer win : 14/22 (63.64%)

To go further

To go further, I will make a tutorial showing you how to implement the RPSLS Game on Android. copyright www.7zhao.net

If you have some comments concerning this tutorial, feel free to use the comments.

copyright www.7zhao.net

本文来自去找www.7zhao.net


本文原文地址:https://medium.com/@ssaurel/creating-a-rock-paper-scissors-game-in-java-with-a-markov-chain-for-the-ai-7672954fd7f6

以上为Creating a Rock Paper Scissors Game in Java with a Markov Chain for the AI文章的全部内容,若您也有好的文章,欢迎与我们分享!

欢迎访问www.7zhao.net

Copyright ©2008-2017去找网版权所有   皖ICP备12002049号-2 皖公网安备 34088102000435号   关于我们|联系我们| 免责声明|友情链接|网站地图|手机版