Lecture (Powerpoint)

AC21001 - Lab 1
The game of nim
Rules of nim
Two players
 Start with N counters
 First player takes 1, 2, or 3 counters
 Second player does the same
 Player who takes last counter loses
 Demo of program

Requirements
Text version of the game
 Ask human who goes first
 For each turn:

Print number of counters left
 If computer’s move, select number and
make move (random or strategy)
 If human’s move, ask how many counters
to take (check for errors)
 Check if game over

Ask for another game
 Keep track of scores

Lab 1: design

Use brainstorming (on your own or with
a friend)
List properties required
 Identify properties that can be implemented
with primitive data types
 Sort them into classes
 Assign other properties to classes
hierarchically


Using your design:
Write C++ class definitions in header files
 Add a Test() method to each class

Candidates for classes
Everything in one class?
 Or....
 Class for one game of nim
 Another class for scores
 Or....
 Classes for computer and human
players
 Another class for game itself

Everything in one class

Properties
Counters left
 Games played
 Games won (by
human)


Actions
Play first?
 Computer move
 Human move
 Adjust scores
 Print scores
 Game over?
 Play again?

Relations
Counters left
Play first?
Games played
Computer move
Adjust scores
Games won
Human move
Print scores
Game over/Play again?
Game & scores classes
Game class

Properties

Counters left

Actions
Play first?
 Computer move
 Human move

Game & scores classes
Scores class

Properties
Games played
 Games won


Actions
Adjust scores
 Print scores
 Play again?

Relations
Game class
Scores class
Counters left
Games won
Games played
Play first?
Adjust scores
Computer move
Print scores
Human move
Game over?
Play again?
Computer/human/game classes
Computer class

Properties

(None)

Actions

Move
Computer/human/game classes
Human class

Properties

(None)

Actions

Move
Computer/human/game classes
Game class

Properties
Counters left
 Games played
 Games won


Actions
Play first?
 Adjust scores
 Print scores
 Game over?
 Play again?

Class communication
Game class must call methods in
scores class
 How to do this?
 Communicate via main( ) method

Communication
Game class
main()
Scores class
Counters left
Games won
Play
Games played
Play first?
Adjust scores
Computer move
Print scores
Human move
Play again?
Game over?
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
Declare a Game pointer
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
Declare a Scores pointer
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
Create Game and
Scores objects
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
Call Play() method
of Game class to start
the game off
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
Play() calls interior
Game class methods
to play game
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
When game ends, Play()
returns human’s score
(1 = win; 0 = lose)
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
“result” is passed to
AdjustScores() method
of Scores class
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Class communication in C++
void main()
{
Game *nimGame;
Scores *nimScores;
int result;
nimGame = new Game;
nimScores = new Scores;
PrintScores method of
Scores class is called
to print scores on screen
// Single play of game...
result = nimGame->Play();
nimScores->AdjustScores(result);
nimScores->PrintScores();
}
Not the best algorithm
Game::Play()
{
HumanPlay();
}
Why is this not an
efficient way of coding
the turns in the game?
Game::HumanPlay()
{
<code for human play>
ComputerPlay();
}
Game::ComputerPlay()
{
<code for computer player>
HumanPlay();
}
Computer nim strategies

Two options:
Play randomly
 Play to win


Random play:


Use random number generator to choose
number of counters (1, 2, 3, or number left)
Winning strategy

Try to work it out...
Input/output in C++
To write to console, use cout
 cout << variable << string << endl;
 To read from console into a variable,
use cin
 cin >> variable1 >> variable 2;

Input/output in C++
#include <iostream>
using namespace std;
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
cin >> AVariable;
cout << "The value of AVariable is: " <<
AVariable << endl;
}
Always include these 2 lines whenever
using cout or cin
Input/output in C++
#include <iostream>
using namespace std;
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
cin >> AVariable;
cout << "The value of AVariable is: " <<
AVariable << endl;
}
Declare an int variable AVariable
Input/output in C++
#include <iostream>
using namespace std;
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
cin >> AVariable;
cout << "The value of AVariable is: " <<
AVariable << endl;
}
Use cout to print a prompt requesting
input
Input/output in C++
#include <iostream>
using namespace std;
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
cin >> AVariable;
cout << "The value of AVariable is: " <<
AVariable << endl;
}
Use cin to read in a value from keyboard
Input/output in C++
#include <iostream>
using namespace std;
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
cin >> AVariable;
cout << "The value of AVariable is: " <<
AVariable << endl;
}
Use cout again to print out the value
Error checking in input

Previous program has a problem:
If non-numeric value entered, goes into
infinite loop
 Cannot provide an error check for incorrect
input


Can solve this with following code
Error-free cin
#include <iostream> cin actually returns a bool flag
using namespace std; indicating whether data was
void main()
read successfully
{
int AVariable;
cout << "Please enter a number" << endl;
while (!(cin >> AVariable))
{
cin.clear();
cin.ignore(80, '\n');
cout << "There was an error!" << endl;
}
cout << "You entered: " << AVariable << endl;
}
Error-free cin
#include <iostream> If cin returns 'false', entered
using namespace std; data was not in correct format
void main()
{
int AVariable;
cout << "Please enter a number" << endl;
while (!(cin >> AVariable))
{
cin.clear();
cin.ignore(80, '\n');
cout << "There was an error!" << endl;
}
cout << "You entered: " << AVariable << endl;
}
Error-free cin
#include <iostream> Clear input buffer, ignore up to
using namespace std; 80 characters (or until \n read)
void main()
and print error message
{
int AVariable;
cout << "Please enter a number" << endl;
while (!(cin >> AVariable))
{
cin.clear();
cin.ignore(80, '\n');
cout << "There was an error!" << endl;
}
cout << "You entered: " << AVariable << endl;
}
Random numbers in Visual C++
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
int main()
{
int i;
Header files needed for
random number
initialization and use
srand( (unsigned)time( NULL ) );
for (i = 0; i < 10; i++)
cout << rand() % 100 + 1 << endl;
}
Random numbers in Visual C++
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
int main()
{
int i;
Must seed the random
number generator – use
the current system time
Note: do this ONCE only
in your program – put
this statement in main()
srand( (unsigned)time( NULL ) );
for (i = 0; i < 10; i++)
cout << rand() % 100 + 1 << endl;
}
Random numbers in Visual C++
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
int main()
{
int i;
rand() is the library method
that generates random ints
between 0 and RAND_MAX
(maximum value defined by
compiler)
srand( (unsigned)time( NULL ) );
for (i = 0; i < 10; i++)
cout << rand() % 100 + 1 << endl;
}
Random numbers in Visual C++
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
int main()
{
int i;
rand() % 100 generates random
ints between 0 and 99, so
add 1 to get numbers between
1 and 100.
[Recall: % is the modulus
operator – returns remainder
when left operand divided by
right operand.]
srand( (unsigned)time( NULL ) );
for (i = 0; i < 10; i++)
cout << rand() % 100 + 1 << endl;
}
Clearing the console screen
To clear the DOS console and position
the cursor at the top left:
 system("cls");
 Can use system() to run any DOS
command
 e.g. system("notepad.exe");
 Use with caution! (e.g. system("del *");)

Lab hand-ins: the program
All labs to be handed in by email
 Code will be compiled and run during
marking
 Make sure you hand in the right version
 Results emailed back to you
 Each lab marked out of 10
 1 mark band off for each day late


e.g. B1  B2
Lab hand-ins: the program
Before emailing your code:
 DELETE THE Debug DIRECTORY!!!
 Use WinZip to zip up all files in your lab
directory
 DON’T FORGET TO INCLUDE YOUR
LAB REPORT (Word file)
 Email Zip file as attachment to:
 [email protected]

Lab hand-ins: the report
Write your report using Word
 Include:

Title of lab
 Purpose of lab (1 or 2 sentences – refer
back to lab sheet for details)
 Your class design, with brief description of
how you arrived at it


Use diagrams if you think it will be clearer
Table showing test results for the program
 List of any bugs you couldn’t fix

Lab hand-ins: testing
Feature
Success?
Data used
Displays welcome
message
Yes
None
Asks if user wants to
play first
Yes
None
Checks if human
Yes
enters valid number of
counters
1,2,3
Checks if human
No
enters valid number of
counters
0
Comments
Accepts 0