-
CS31 Discussion 1H Notes (Week 10, June 5)
June 4th, 2009
CS31 Discussion 1H Notes (Week 10, June 5) are available.
All files in the notes directory can be browsed by going directly to http://stuff.abacadaba.com/
The code for notes can be browsed by going directly to http://stuff.abacadaba.com/code -
CS31 C++ – string Player::takeComputerChosenTurn()
June 4th, 2009
The notion of “replacement implementation should do something intelligent” is pretty vague. However if you look at the test code there are these lines:
for (int k = 0; k < 1000 && a.robotCount() != 0; k++)
pp->takeComputerChosenTurn();
assert(a.robotCount() == 0);Translated to English these three lines say after 1000 turns takeComputerChosenTurn() should have destroyed all the robots. So one can’t possibly meet that if you don’t destroy robots. Further you must survive long enough to destroy all the robots.
If after the robots move, a robot occupies the same grid point as the player, the player dies. Since the robot moves randomly there is a 1 in 4 chance for the robot to kill you.
Therefore you better move if there is a possibility for the robot to kill you. Further if it is not possible to move away from multiple robots then moving to a spot with the least chance of killing you would be good.
Finally if you are need in immediate danger then shooting the nearest robot would be good because without shooting you won’t destroy all the robots.
for (int k = 0; k < 1000 && a.robotCount() != 0; k++)
pp->takeComputerChosenTurn();
assert(a.robotCount() == 0);// Your replacement implementation should do something intelligent
// and return a string that describes what happened. When you’ve
// decided what action to take, take it by calling move, shoot, or stand.
// This function must return one of the following four strings:
// “Moved.”
// “Shot and hit!”
// “Shot and missed!”
// “Stood.”// Here’s one possible strategy:
// If moving in some direction would put me in less immediate danger
// than standing, then move in that direction.
// else shoot in the direction of the nearest robot I can hit.// A more aggressive strategy is possible, where you hunt down robots.
-
CS31 C++ – looping thru an array of pointers to robots
June 3rd, 2009
The professor has examples of looping thru an array of pointers to robots in his code. For example, in Arena::~Arena() he deletes all the robots.
for (int k = 0; k < m_nRobots; k++)
delete m_robots[k];Arena::~Arena()
{
for (int k = 0; k < m_nRobots; k++)
delete m_robots[k];
delete m_player;
} -
CS31 C++ – void Arena::display(string msg) const
June 3rd, 2009
Remember that char grid[MAXROWS][MAXCOLS] is populated with ‘.’
for (r = 0; r < rows(); r++)
for (c = 0; c < cols(); c++)
grid[r][c] = '.';... so if you place a robot at a particular column and row
you can check what is there ... if it is a . you know that this is the FIRST robot you are placing there ... if it is an 'R' than this is the second ... similar logic can be used for placing the player.// TODO: If one robot is at some grid point, set the char to 'R'.
// If it's 2 though 8, set it to '2' through '8'.
// For 9 or more, set it to '9'.void Arena::display(string msg) const
{
char grid[MAXROWS][MAXCOLS];// TODO: If one robot is at some grid point, set the char to ‘R’.
// If it’s 2 though 8, set it to ‘2′ through ‘8′.
// For 9 or more, set it to ‘9′.// Indicate player’s position
if (m_player != NULL)
{
// Set the char to ‘@’, unless there’s also a robot there,
// in which case set it to ‘*’ -
CS31 C++ – Project 7 Basic Tests
June 3rd, 2009
The prof posted “If your code passes these basic tests, you’ll probably do well on Project 7.”
-
CS 31 C++ – void Robot::move()
June 2nd, 2009
Remember that void Arena::display(string msg) const draws the robots on the grid. The robot does not move itself.
Arena::display(string msg) asks the robot where it is and draws it there.void Robot::move()
{
// Attempt to move in a random direction; if we can’t move, don’t move
switch (rand() % 4)
{
case UP:
// TODO: Move the robot up one row if possible.
break;
case DOWN:
case LEFT:
case RIGHT:
// TODO: Implement the other movements.
break;
}
} -
CS 31 C++ – Practice Problems
June 2nd, 2009
Every chapter in the Savitch book has “self-test problems” and the answers to the self-test problems are in the back of the chapter.
Do at least a couple problems from each chapter and more for those chapters for which the material is unclear to you. If you have problems/questions with the Savitch self-test problems then ask.
-
CS 31 C++ – Final Cheat Sheet Template
June 2nd, 2009
CS 31 C++ – Final Cheat Sheet Template
Note: This document is for ideas for YOU to create your own Final Cheat Sheet
This is The BEGINNING of a Final Cheat Sheet. YOU should edit it, add to it, subtract from it based on what you think you might need reminders of on the test.- CS 31 Final Cheat Sheet Template (Week 10, June 1)(.PDF)
- CS 31 Final Cheat Sheet Template (Week 10, June 1)(.doc.zip)
- CS 31 Final Cheat Sheet Template (Week 10, June 1)(.doc)
- CS 31 Final Cheat Sheet Template (Week 10, June 1)(.txt)
All files in the notes directory can be browsed by going directly to http://stuff.abacadaba.com/
-
Debugging Project 7
June 1st, 2009
You can always create objects in your main to test things.Say you are working on int Arena::rows() const
int Arena::rows() const
{
// TODO: TRIVIAL: return the number of rows in the arena
return 1; // this is wrong — replace it
}
then your main could have something likeArena a(13,7);cout << a.rows() << endl; -
CS31 C++ – Final Pre-review will also discuss Project 7
June 1st, 2009
Are students who are not part of your discussion allowed to attend the review session today at 7?
Yes