473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Modifying 13 stones game to force computer to make specific first move

I already did the game once and it works but now I need to change it to add a function that will make the computer take player 1 turn and always win.
heres the original
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2.  
  3. void main()
  4. {
  5.     int nums, // stones left
  6.         nump,   // Number of stones picked up by player
  7.         player;
  8.               // Player identifier (1 or 2)
  9.  
  10.  
  11.     cout << endl; 
  12.     cout << "You have before you a pile of 13 stones. "
  13.          << "Players 1 and 2 alternate" << endl;
  14.     cout << "picking from 1 to 3 stones off the pile. "
  15.          << "The last player to pick up " << endl;
  16.     cout << "a stone is the winner. Good luck."  << endl;
  17.     cout << endl;
  18.  
  19.  
  20.     nums = 13;
  21.     player = 1;
  22.  
  23.     while ( nums>0 )   // While there is a stone remaining
  24.     {
  25.  
  26.        cout<<"Pick stones player number "<<player<<" : "; // Prompt player to pick up 1-3 stones.
  27.        cin>>nump;
  28.  
  29.     if( nump>0&&nump<4)
  30.      {
  31.       nums-=nump;
  32.       if (nums<1)
  33.       {
  34.  
  35.     cout << "No stones left -- "
  36.          << "player " << player << " is the winner!" << endl;
  37.          }
  38.       cout<<nums<<" stones are left\n";
  39.        player++;
  40.          if(player>2)
  41.     {
  42.       player=1; 
  43.      }  
  44.  
  45.      }
  46.      else
  47.      {
  48.      cout<<"Cheata\n";
  49.      }
  50.  
  51.      // If move is legal, then subtract the number of stones played
  52.         // and change players (if a win has not occurred).
  53.  
  54.  
  55.  
  56.  
  57.     }
  58.  
  59.  
  60.  
  61. }
  62.  
How do I do it
Jan 8 '08 #1
2 1994
sicarie
4,677 Expert Mod 4TB
So there are two parts to this - the function declaration, and the implementation.

Do you know how to create a function "prototype"?

From there, you need to define your algorithm to make the computer move the way you want it, and then (I'm guessing) turn it over to the user.
Jan 9 '08 #2
RRick
463 Expert 256MB
You first will need an algorithm (basically a generalized idea) that the computer will use to figure out how many stones to pick. Once you figure that out, you write the routine to implement the algorithm.

The trick here is to realize that whoever gets to pick with 4 stones in a pile looses. To win, you want 3 * n + 1 stones (where n=1,2,3,...) MORE than 4 stones in the pile. Once there are less that 4 stones, you take the rest and win.

You can write an algorithm that calculates the the number to remove from a given value from the above idea. Since you don't have many rocks to work with, you can create a table (from the above algor) that tells the computer how many rocks should be left after it removes some rocks. In this case, you find the largest number in the table that is less than the current number
Jan 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: me | last post by:
Hey all, I am making a 4 in a row game in flash MX. Wel i do it a bit pimitief like sending respons from the swf file to the php-mysql handler to see if the other user has make a move. I...
23
by: coinjo | last post by:
In "Thirteen Stones" game, two players alternately take 1, 2, or 3 stones from a pile of 13 stones until no stones are left. The last player to pick up a stone is the winner. I need to make a...
2
by: Brian Basquille | last post by:
Hello all. Air Hockey game is due up in just over a month. And i've about 2 and a half weeks to work on it. So, i need some feedback and advice. ...
10
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
4
by: Hypnotik | last post by:
So I'm writing this program. I have the board constructed, and I'm having a problem making a move. At this point I just want to make a move, I'll work on whether it is a legal move after that. The...
10
by: j0k3r | last post by:
Hiya, i'm trying to create a Go (the game) simulation in PHP, the problem is how to mark dead stones. each stone has some liberties wich are the empty territories nearby, when a group of stones...
14
by: v4vijayakumar | last post by:
In computer based, two player, board games, how to make computer play? Are there any formal ways to _teach_ computer, to choose best possible move? I know this is kind of off-topic here. Please...
1
by: Leiram | last post by:
I am trying to write a game where there is 13 stones and you play against the computer to make sure that you don't take the last stone. You or the computer (depending on the turn) is allowed to take...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.