SlideShare a Scribd company logo
Mouse and Cat Game In C++
Mouse and Cat game in C++
This is a mouse and cat game
controlled by specific characters to
play the game. Their positions are
controlled randomly throughout the
game.
#include "MouseAndCatGame.h"
MouseAndCatGame::MouseAndCatGame(Inter
face* pui, Player* pplayer)
:underground_(), p_ui(pui),
p_player(pplayer), cheatModeOn(false)
{
//prepare game
//mouse state already set up in
its contructor
//set up cat
cat_.spotMouse(&mouse_);
postionAtRandom();
}
const RandomNumberGenerator
MouseAndCatGame::rng_;
void MouseAndCatGame::run() {
p_ui-
>drawGridOnScreen(prepareGrid());
key_ = p_ui-
>getKeyPressFromUser();
const int KEY_C = 67;
while (!hasEnded(key_))
{
if (isArrowKeyCode(key_))
{
mouse_.scamper(key_);
if (!cheatModeOn)
cat_.chaseMouse();
p_ui-
>drawGridOnScreen(prepareGrid());
applyRules();
}
else if (key_ == KEY_C)
{
// toggle the cheat
mode
cheatModeOn =
!cheatModeOn;
p_ui-
>drawGridOnScreen(prepareGrid());
}
key_ = p_ui-
>getKeyPressFromUser();
}
p_ui-
>showResultsOnScreen(prepareEndMessage
());
}
string MouseAndCatGame::prepareGrid()
const{
//prepare a string that holds
the grid information
ostringstream os;
for (int row(1); row <= SIZE;
++row) //for each row (vertically)
{
for (int col(1); col <=
SIZE; ++col) //for each column
(horizontally)
{
if ((row == cat_.y_)
&& (col == cat_.x_))
os <<
cat_.getSymbol(); //show cat
else
if ((row ==
mouse_.getY()) && (col ==
mouse_.getX()))
os <<
mouse_.getSymbol(); //show mouse
else
if
((nut_.hasBeenCollected() == false) &&
(row == nut_.getY()) && (col ==
nut_.getX()))
os <<
nut_.getSymbol();
else
{
if
(underground_.checkForHole(col, row))
os <<
underground_.getHoleSymbol();
else
os << FREECELL; //show free grid
cell
}
} //end of col-loop
os << endl;
} //end of row-loop
os << endl << p_player-
>getName() << " Score: " <<
p_player->getScoreAmount();
if (nut_.hasBeenCollected())
os <<
collectedNutMessage();
if (cheatModeOn)
os << "nCheat Mode
Activated!";
return os.str();
} //end prepareGrid
bool
MouseAndCatGame::isArrowKeyCode(const
int keycode) const{
return (keycode == LEFT) ||
(keycode == RIGHT) || (keycode == UP)
|| (keycode == DOWN);
}
void MouseAndCatGame::applyRules(){
if (cat_.hasCaughtMouse())
{
mouse_.die();
p_player-
>updateScoreAmount(-1);
}
else if (mouse_.hasNut(&nut_) &&
mouse_.hasReachedAHole(&underground_))
{
mouse_.escapeIntoHole();
p_player-
>updateScoreAmount(1);
}
}
bool MouseAndCatGame::hasEnded(char
key) const{
return ((key == QUIT) ||
(!mouse_.isAlive()) ||
(mouse_.hasEscaped()));
}
string
MouseAndCatGame::collectedNutMessage()
const{
return "nTHE NUT HAS BEEN
COLLECTED";
}
string
MouseAndCatGame::prepareEndMessage()
const{
ostringstream os;
if (mouse_.hasEscaped())
os << "nnEND OF GAME: THE
MOUSE ESCAPED UNDERGROUND!";
else
if (!mouse_.isAlive())
os << "nnEND OF
GAME: THE CAT ATE THE MOUSE!";
else
os << "nnEND OF
GAME: THE PLAYER ENDED THE GAME!";
return os.str();
}
void
MouseAndCatGame::postionAtRandom(){
int x = 0;
int y = 0;
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
nut_.setPosition(x, y);
} while
(underground_.checkForHole(x, y));
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
mouse_.setPosition(x, y);
} while
((underground_.checkForHole(x, y)) ||
(nut_.isAtPosition(x, y)));
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
cat_.setPosition(x, y);
} while
((underground_.checkForHole(x, y)) ||
(nut_.isAtPosition(x, y)) ||
(mouse_.isAtPosition(x, y)));
}
ostream&
MouseAndCatGame::toFile(ostream& os)
const{
os << setfill('0')
<< setw(2) << mouse_.getX()
<< "/"
<< setw(2) << mouse_.getY()
<< "/"
<< setw(2) << cat_.getX()
<< "/"
<< setw(2) << cat_.getY()
<< "/"
<< setw(2) << nut_.getX()
<< "/"
<< setw(2) << nut_.getY()
<< "/"
<< setw(2) <<
nut_.hasBeenCollected() << "/";
return os;
}
istream&
MouseAndCatGame::fromFile(istream&
is){
char ch;
is >> mouse_.x_ >> ch
>> mouse_.y_ >> ch
>> cat_.x_ >> ch
>> cat_.y_ >> ch
>> nut_.x_ >> ch
>> nut_.y_ >> ch
>> nut_.collected_ >>ch;
return is;
}
ostream& operator<<(ostream& os, const
MouseAndCatGame& mcg){
return (mcg.toFile(os));
}
istream& operator>>(istream& is,
MouseAndCatGame& mcg){
return (mcg.fromFile(is));
}
Mouse and Cat Game In C++

More Related Content

DOCX
Create xo game in android studio
PDF
Flappy bird
PDF
The Ring programming language version 1.10 book - Part 71 of 212
PDF
Python Developer's Daily Routine
PDF
PDF
RandomGuessingGame
Create xo game in android studio
Flappy bird
The Ring programming language version 1.10 book - Part 71 of 212
Python Developer's Daily Routine
RandomGuessingGame

What's hot (20)

PPT
Linux mouse
PDF
201707 CSE110 Lecture 13
PDF
The Ring programming language version 1.5.3 book - Part 79 of 184
PDF
The Ring programming language version 1.6 book - Part 61 of 189
PDF
The Ring programming language version 1.2 book - Part 50 of 84
PPTX
TicketBEKA? Ticket booking
KEY
20110424 action scriptを使わないflash勉強会
PDF
The Ring programming language version 1.3 book - Part 42 of 88
PDF
PyCon2009_AI_Alt
PDF
The Ring programming language version 1.5.2 book - Part 50 of 181
PDF
The Ring programming language version 1.4.1 book - Part 15 of 31
DOC
เกมจับคู่1
PDF
The Ring programming language version 1.6 book - Part 53 of 189
PDF
The Ring programming language version 1.8 book - Part 57 of 202
PDF
Python Data-Science-Preview
PDF
2c astable monostable
PDF
The Ring programming language version 1.5.1 book - Part 49 of 180
PPTX
Project 2
PDF
The Ring programming language version 1.5.3 book - Part 62 of 184
PDF
Fizzbuzzalooza
Linux mouse
201707 CSE110 Lecture 13
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.6 book - Part 61 of 189
The Ring programming language version 1.2 book - Part 50 of 84
TicketBEKA? Ticket booking
20110424 action scriptを使わないflash勉強会
The Ring programming language version 1.3 book - Part 42 of 88
PyCon2009_AI_Alt
The Ring programming language version 1.5.2 book - Part 50 of 181
The Ring programming language version 1.4.1 book - Part 15 of 31
เกมจับคู่1
The Ring programming language version 1.6 book - Part 53 of 189
The Ring programming language version 1.8 book - Part 57 of 202
Python Data-Science-Preview
2c astable monostable
The Ring programming language version 1.5.1 book - Part 49 of 180
Project 2
The Ring programming language version 1.5.3 book - Part 62 of 184
Fizzbuzzalooza
Ad

Viewers also liked (8)

PPTX
Jess Adaptation Hunger Games
PPTX
Enrichment Next Steps - PowerPoint 2
PPT
Games C L
PDF
Word games in c
DOC
Basic version of MS Paint created using Turbo C++
PPTX
1 c – graphic designs
PPT
C ppt
PPSX
INTRODUCTION TO C PROGRAMMING
Jess Adaptation Hunger Games
Enrichment Next Steps - PowerPoint 2
Games C L
Word games in c
Basic version of MS Paint created using Turbo C++
1 c – graphic designs
C ppt
INTRODUCTION TO C PROGRAMMING
Ad

Similar to Mouse and Cat Game In C++ (12)

DOCX
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
DOCX
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
PDF
Description For this part of the assignment, you will create a Grid .pdf
PDF
implemement the game.cpp by the header file given. And create main.c.pdf
PDF
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
PDF
#includeiostream #includetime.h #includecstdio #include.pdf
PDF
you will use cellular automata to create a 2D predator–prey simulati.pdf
DOCX
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
DOCX
Magic Game ClassesMagicAnimalTriviaTrivia.cppMagic Game Classe.docx
TXT
Tic tac toe
PDF
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
PPTX
Snake game with c++ and the tools are codeblocks(IDE) and windows operating s...
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Description For this part of the assignment, you will create a Grid .pdf
implemement the game.cpp by the header file given. And create main.c.pdf
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
#includeiostream #includetime.h #includecstdio #include.pdf
you will use cellular automata to create a 2D predator–prey simulati.pdf
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
Magic Game ClassesMagicAnimalTriviaTrivia.cppMagic Game Classe.docx
Tic tac toe
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
Snake game with c++ and the tools are codeblocks(IDE) and windows operating s...

More from Programming Homework Help (7)

PDF
Java Assignment Help
PDF
C# Assignmet Help
PDF
C# Assignmet Help
PDF
Family tree in java
PDF
Binary tree in java
PDF
Bank account in java
PDF
Card Games in C++
Java Assignment Help
C# Assignmet Help
C# Assignmet Help
Family tree in java
Binary tree in java
Bank account in java
Card Games in C++

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
master seminar digital applications in india
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
master seminar digital applications in india
Sports Quiz easy sports quiz sports quiz
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Supply Chain Operations Speaking Notes -ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers

Mouse and Cat Game In C++

  • 2. Mouse and Cat game in C++ This is a mouse and cat game controlled by specific characters to play the game. Their positions are controlled randomly throughout the game. #include "MouseAndCatGame.h" MouseAndCatGame::MouseAndCatGame(Inter face* pui, Player* pplayer) :underground_(), p_ui(pui), p_player(pplayer), cheatModeOn(false) { //prepare game //mouse state already set up in its contructor //set up cat cat_.spotMouse(&mouse_); postionAtRandom(); } const RandomNumberGenerator MouseAndCatGame::rng_; void MouseAndCatGame::run() { p_ui- >drawGridOnScreen(prepareGrid()); key_ = p_ui- >getKeyPressFromUser();
  • 3. const int KEY_C = 67; while (!hasEnded(key_)) { if (isArrowKeyCode(key_)) { mouse_.scamper(key_); if (!cheatModeOn) cat_.chaseMouse(); p_ui- >drawGridOnScreen(prepareGrid()); applyRules(); } else if (key_ == KEY_C) { // toggle the cheat mode cheatModeOn = !cheatModeOn; p_ui- >drawGridOnScreen(prepareGrid()); } key_ = p_ui- >getKeyPressFromUser(); } p_ui- >showResultsOnScreen(prepareEndMessage ()); } string MouseAndCatGame::prepareGrid() const{ //prepare a string that holds the grid information ostringstream os; for (int row(1); row <= SIZE; ++row) //for each row (vertically) {
  • 4. for (int col(1); col <= SIZE; ++col) //for each column (horizontally) { if ((row == cat_.y_) && (col == cat_.x_)) os << cat_.getSymbol(); //show cat else if ((row == mouse_.getY()) && (col == mouse_.getX())) os << mouse_.getSymbol(); //show mouse else if ((nut_.hasBeenCollected() == false) && (row == nut_.getY()) && (col == nut_.getX())) os << nut_.getSymbol(); else { if (underground_.checkForHole(col, row)) os << underground_.getHoleSymbol(); else os << FREECELL; //show free grid cell } } //end of col-loop os << endl; } //end of row-loop
  • 5. os << endl << p_player- >getName() << " Score: " << p_player->getScoreAmount(); if (nut_.hasBeenCollected()) os << collectedNutMessage(); if (cheatModeOn) os << "nCheat Mode Activated!"; return os.str(); } //end prepareGrid bool MouseAndCatGame::isArrowKeyCode(const int keycode) const{ return (keycode == LEFT) || (keycode == RIGHT) || (keycode == UP) || (keycode == DOWN); } void MouseAndCatGame::applyRules(){ if (cat_.hasCaughtMouse()) { mouse_.die(); p_player- >updateScoreAmount(-1); } else if (mouse_.hasNut(&nut_) && mouse_.hasReachedAHole(&underground_)) { mouse_.escapeIntoHole(); p_player- >updateScoreAmount(1); } } bool MouseAndCatGame::hasEnded(char key) const{
  • 6. return ((key == QUIT) || (!mouse_.isAlive()) || (mouse_.hasEscaped())); } string MouseAndCatGame::collectedNutMessage() const{ return "nTHE NUT HAS BEEN COLLECTED"; } string MouseAndCatGame::prepareEndMessage() const{ ostringstream os; if (mouse_.hasEscaped()) os << "nnEND OF GAME: THE MOUSE ESCAPED UNDERGROUND!"; else if (!mouse_.isAlive()) os << "nnEND OF GAME: THE CAT ATE THE MOUSE!"; else os << "nnEND OF GAME: THE PLAYER ENDED THE GAME!"; return os.str(); } void MouseAndCatGame::postionAtRandom(){ int x = 0; int y = 0; do { x = rng_.getRandomValue(SIZE);
  • 7. y = rng_.getRandomValue(SIZE); nut_.setPosition(x, y); } while (underground_.checkForHole(x, y)); do { x = rng_.getRandomValue(SIZE); y = rng_.getRandomValue(SIZE); mouse_.setPosition(x, y); } while ((underground_.checkForHole(x, y)) || (nut_.isAtPosition(x, y))); do { x = rng_.getRandomValue(SIZE); y = rng_.getRandomValue(SIZE); cat_.setPosition(x, y); } while ((underground_.checkForHole(x, y)) || (nut_.isAtPosition(x, y)) || (mouse_.isAtPosition(x, y))); } ostream& MouseAndCatGame::toFile(ostream& os) const{ os << setfill('0') << setw(2) << mouse_.getX() << "/"
  • 8. << setw(2) << mouse_.getY() << "/" << setw(2) << cat_.getX() << "/" << setw(2) << cat_.getY() << "/" << setw(2) << nut_.getX() << "/" << setw(2) << nut_.getY() << "/" << setw(2) << nut_.hasBeenCollected() << "/"; return os; } istream& MouseAndCatGame::fromFile(istream& is){ char ch; is >> mouse_.x_ >> ch >> mouse_.y_ >> ch >> cat_.x_ >> ch >> cat_.y_ >> ch >> nut_.x_ >> ch >> nut_.y_ >> ch >> nut_.collected_ >>ch; return is; } ostream& operator<<(ostream& os, const MouseAndCatGame& mcg){ return (mcg.toFile(os)); } istream& operator>>(istream& is, MouseAndCatGame& mcg){ return (mcg.fromFile(is)); }