
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.
系统特色及功能简介,主要包括以下方面: 合一:包括语言、模板风格、用户群;此版本内订简体、繁体、英文于一体;可另增设其它语言选项;模板风格指可以存在多界面的情况下进行界面互换;用户群指可写于单用户版本,也可用于多用户商城版本,具体设置可通过会员组权限修改 会员组定制:系统初安装时,内订6级会员分组,即 游客组、管理员组、VIP用户组、柜台用户组、柜台VIP用户组;此6级会员组不可以删除。另管理
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
#includeusing 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 < n; i++) { if (arr[i] % 3 == 0 && arr[i] % 5 == 0) movesBoth++; else if (arr[i] % 3 == 0) movesA++; else if (arr[i] % 5 == 0) movesB++; } if (movesBoth == 0) { if (movesA > movesB) cout<<"Player 1 is the Winner"; cout<<"Player 2 is the Winner"; } if (movesA + 1 > movesB) cout<<"Player 1 is the Winner"; cout<<"Player 2 is the Winner"; ; return 0; }
输出
Player 2 is the Winner










