Modify the program so that the computer plays a more informed game.

Modify the program so that the computer plays a more informed game.

LANGUAGE: C++

CHALLENGE:

The computer player in Programming Project 15.5 does not play the number guessing game very well, since it makes only random guesses. Modify the program so that the computer plays a more informed game. The specific strategy is up to you, but you must add function(s) to the Player and ComputerPlayer classes so that the play(Player &player1, Player &player2) fucntion can send the results of a guess back to the computer player. In other words, the computer must be told if its guess was too high or too lo, and it alos must be told if its opponents last guess was too high or too low. The computer can use the information to revise its next guess.

SOLUTION:

computerPlayer.cpp

</p>
<p>#include &quot;computerPlayer.h&quot;<br />
#include<br />
using namespace std;</p>
<p>ComputerPlayer::ComputerPlayer() : currentMax(99), currentMin(0){}</p>
<p>int ComputerPlayer::getGuess(){<br />
	int guess;<br />
	guess  = (currentMax + currentMin)/2;<br />
	return guess;<br />
}</p>
<p> void ComputerPlayer::feedback(int guess, bool tooHigh){<br />
	 if (tooHigh &amp;&amp; guess &lt; currentMax){<br />
 		 currentMax = guess;<br />
 	 }else if (!tooHigh &amp;&amp; guess &gt; currentMin){<br />
		 currentMin = guess;<br />
	 }<br />
 }</p>
<p> void ComputerPlayer::reset(){<br />
	currentMax = 99;<br />
	currentMin = 0;<br />
 }</p>
<p>

computerPlayer.h

</p>
<p>#ifndef COMPUTERPLAYER_H<br />
#define COMPUTERPLAYER_H</p>
<p>#include &quot;Player.h&quot;</p>
<p>class ComputerPlayer : public Player{<br />
	public:<br />
		ComputerPlayer();<br />
		virtual int getGuess();<br />
		virtual void feedback(int guess, bool tooHigh);<br />
		virtual void reset();</p>
<p>	protected:<br />
		int currentMax;<br />
		int currentMin;<br />
};</p>
<p>#endif</p>
<p>

game.cpp

</p>
<p>#include<br />
#include<br />
#include &quot;Player.h&quot;<br />
#include &quot;HumanPlayer.h&quot;<br />
#include &quot;computerPlayer.h&quot;<br />
#include &quot;time.h&quot;<br />
using namespace std;</p>
<p>void checkForWin(int guess, int answer, bool&amp; win, bool&amp; tooHigh);<br />
void play(Player &amp;player1, Player &amp;player2);</p>
<p>int main(int argc, char *argv[] ){<br />
	string p1, p2;</p>
<p>	if (argc == 3){<br />
		p1 = argv[1];<br />
		p2 = argv[2];<br />
	}else{<br />
		cout &lt;&lt; &quot;2 arguements were not passed: Default to h vs c\n\n&quot;;<br />
		p1 = &quot;h&quot;;<br />
		p2 = &quot;c&quot;;<br />
	}</p>
<p>	if ((p1 == &quot;h&quot;) &amp;&amp; (p2 == &quot;h&quot;)){<br />
		HumanPlayer player1;<br />
		HumanPlayer player2;<br />
		play(player1, player2);<br />
	}else if ((p1 == &quot;h&quot;) &amp;&amp; (p2 == &quot;c&quot;)){<br />
		HumanPlayer player1;<br />
		ComputerPlayer player2;<br />
		play(player1, player2);<br />
	}else if ((p1 == &quot;c&quot;) &amp;&amp; (p2 == &quot;h&quot;)){<br />
		ComputerPlayer player1;<br />
		HumanPlayer player2;<br />
		play(player1, player2);<br />
	}else if ((p1 == &quot;c&quot;) &amp;&amp; (p2 == &quot;c&quot;)){<br />
		ComputerPlayer player1;<br />
		ComputerPlayer player2;<br />
		play(player1, player2);<br />
	}else{<br />
		cout &lt;&lt; &quot;Invalid arguements: Default to h vs c\n&quot;;<br />
		HumanPlayer player1;<br />
		ComputerPlayer player2;<br />
		play(player1, player2);<br />
	}</p>
<p>	system(&quot;PA-- USE&quot;);<br />
	return 0;<br />
}</p>
<p>void checkForWin(int guess, int answer, bool&amp; win, bool&amp; tooHigh){<br />
	cout &lt;&lt; &quot;You guessed &quot; &lt;&lt; guess &lt;&lt; &quot;.  &quot;;<br />
	if (answer == guess){<br />
		cout &lt;&lt; &quot;You're right!  You win!&quot; &lt;&lt; endl;<br />
		win = true;<br />
	}else if ( answer &lt; guess ){<br />
		cout &lt;&lt; &quot;Your guess is too high.&quot; &lt;&lt; endl;<br />
		tooHigh = true;<br />
	}else{<br />
		cout &lt;&lt; &quot;Your guess is too low.&quot; &lt;&lt; endl;<br />
		tooHigh = false;<br />
	}<br />
}</p>
<p>void play(Player &amp;player1, Player &amp;player2){<br />
	char ans;</p>
<p>	do{<br />
		player1.reset();<br />
		player2.reset();</p>
<p>		int answer = 0, guess = 0;<br />
		srand(time(NULL));<br />
		answer = rand() % 100;<br />
		bool win = false, tooHigh = false;</p>
<p>		while (!win){<br />
			cout &lt;&lt; &quot;Player 1's turn to guess.&quot; &lt;&lt; endl;<br />
			guess = player1.getGuess();<br />
			checkForWin(guess, answer, win, tooHigh);<br />
			if (win) {<br />
				break;  //exit while loop if player 1 wins<br />
			}<br />
			player1.feedback(guess, tooHigh);<br />
			player2.feedback(guess, tooHigh);</p>
<p>			cout &lt;&lt; &quot;Player 2's turn to guess.&quot; &lt;&lt; endl;<br />
			guess = player2.getGuess();<br />
			checkForWin(guess, answer, win, tooHigh);<br />
			player1.feedback(guess, tooHigh);<br />
			player2.feedback(guess, tooHigh);<br />
		}</p>
<p>		cout &lt;&lt; &quot;Do you want to play again?  (y/n)&quot;; 		cin &gt;&gt; ans;</p>
<p>	} while ( (ans == 'y') || (ans == 'Y') );<br />
}<br />

HumanPlayer.cpp

</p>
<p>#include &quot;HumanPlayer.h&quot;<br />
#include<br />
using namespace std;</p>
<p>int HumanPlayer::getGuess(){<br />
	int guess;</p>
<p>	do{<br />
		cout &lt;&lt; &quot;Enter guess between 0 and 99.\n&quot;; 		cin &gt;&gt; guess;</p>
<p>	} while ( guess &lt; 0 || guess &gt; 99 );</p>
<p>	return guess;<br />
}</p>
<p>

HumanPlayer.h

</p>
<p>#ifndef HUMANPLAYER_H<br />
#define HUMANPLAYER_H</p>
<p>#include &quot;Player.h&quot;</p>
<p>class HumanPlayer : public Player{<br />
	public:<br />
		virtual int getGuess();<br />
};</p>
<p>#endif</p>
<p>

Player.cpp

</p>
<p>#include &quot;Player.h&quot;</p>
<p>int Player::getGuess(){<br />
	return 0;<br />
}</p>
<p>void Player::feedback(int guess, bool tooHigh){}</p>
<p>void Player::reset(){}</p>
<p>

Player.h

</p>
<p>#ifndef PLAYER_H<br />
#define PLAYER_H</p>
<p>class Player{<br />
	public:<br />
		virtual int getGuess();<br />
		virtual void feedback(int guess, bool tooHigh);<br />
		virtual void reset();<br />
};</p>
<p>#endif</p>
<p>

Add comment