Modified game of Nim is an optimisation games of arrays. This game predicts the winner based on the starting player and optimal moves.
Game Logic − In this game, we are given an array{}, that contains elements. There are generally two players that play the game namly player1 and player2. The aim of both is to make sure that all their numbers are removed from the array. Now, player1 has to remove all the numbers that are divisible by 3 and the player2 has to remove all the numbers that are divisible by 5. The aim is to make sure that they remove all elements optimally and find the winner in this case.
Sample
Array : {1,5, 75,2,65,7,25,6}Winner : playerB.A removes 75 -> B removes 5 -> A removes 6 -> B removes 65 -> No moves for A, B wins.
登录后复制
Code Preview
The code will find the number of elements that A can remove , number of elements that B can remove and the number of elements that they both can remove. Based on the number of the elements they both can remove the solution is found. As A removes first elements it can win even if he has to remove one element more than B. In normal case, the player with the maximum number of elements to remove wins.
PROGRAM TO FIND THE SOLUTION FOR GAME OF NIM
#include using namespace std;int main() { int arr[] = {1,5, 75,2,65,7,25,6}; int n = sizeof(arr) / sizeof(arr[0]); int movesA = 0, movesB = 0, movesBoth = 0; for (int i = 0; i movesB) cout movesB) cout输出
Player 2 is the Winner登录后复制
以上就是一个用C语言修改过的Nim游戏?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584393.html