Toads and Frogs Game!

This game was a C++ coursework task we where assigned in college. I had never actually heard of it before so I thought it would be an interesting game to share here. Please note, this was my first assignment for C++ so the practise used may not be the best, but if you do have any suggestions i’d love to hear them.

Code:


#include <iostream>

using namespace std;

// declaring functions
void showMessage();
void startGame();
void playerMove();
void winner();
void restartGame();
bool playToad(int n);
bool playFrog(int n);

//setting how many places on board
const int N = 6;

//moves for game
const int MOVES [N] = {0,1,2,3,4,5};

//game characters
char gameCharacters[N] = {'T','T', '.', '.', 'F', 'F'};

//ending loop for running game
bool gameOver = false;

//whos turn is it
int player = 1;

int main()
{
    //calling the function to print out for the start menu
    startGame();
    do
	{
        //calling functions for to see who won and to see whos go it is
        winner();
        playerMove();
        
		string s="";
        
        //reading in users input
		getline(cin,s);
		if (s.length()>0)
		{
            switch(s[0])
            {
                case '0':
                {
                    //finding out which player it is and then calling function
                    if (player ==1)
                    {
                        playToad(0);
                        
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(0);
                        player --;
                    }//else
                    
                }//case0
                    
                    break;
                    
                case '1':
                {
                    if (player ==1)
                    {
                        playToad(1);
                        
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(1);
                        player --;
                    }//else
                }//case1
                    
                    break;
                    
                case '2':
                {
                    if (player ==1)
                    {
                        playToad(2);
                        
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(2);
                        player --;
                    }//else
                    
                }//case 2
                    
                    break;
                    
                case '3':
                {
                    if (player ==1)
                    {
                        playToad(3);
                        
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(3);
                        player --;
                    }//else
                    
                }//case 3
                    break;
                    
                case '4':
                {
                    if (player ==1)
                    {
                        playToad(4);
                        
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(4);
                        player --;
                    }//else
                    
                }//case 4
                    break;
                    
                case '5':
                {
                    if (player ==1)
                    {
                        playToad(5);
                        player ++;
                        
                    }//if
                    
                    else
                    {
                        playFrog(5);
                        player --;
                    }//else
                    
                }//case 5
                    break;
                    
                case 'q':
                {
                    exit(0);// quit the program
                }//case q
                    break;
                    
                case 'r':
                {
                    restartGame(); // resarting
                }//case r
                    break;
                    
                default:
                {
                    showMessage();
                }//default
                    
            }//switch
            
        }//if
        
	}//do
    
    while(gameOver == false);
    
    return 0;
}//main

bool playToad(int n)
{
    if(gameCharacters[n]== 'T')
    {
        if(n<N-1)
        {
            // if the space is empty
            if(gameCharacters[n+1]=='.')
            {
                //moving
                gameCharacters[n] = '.';
                gameCharacters[n+1]='T';
                
                //printout new array
                for(int count=0; count<N; count++)
                {
                    cout << gameCharacters[count];
                }//for
                
                cout << endl;
                
                return true;
            }//if
            
            if(n<N-2)
            {
                if(gameCharacters[n+2]=='.')
                {
                    //moving
                    gameCharacters[n] = '.';
                    gameCharacters[n+2]='T';
                    
                    for(int count=0; count<N; count++)
                    {
                        cout << gameCharacters[count];
                    }//for
                    
                    cout << endl;
                    
                    return true;
                }//if
                
                else
                {
                    return false;
                }//else
                
            }//if
            
            else
            {
                return false;
            }//else
            
        }//if
        
        else
        {
            return false;
        }//else
    }//if
    
    else
    {
        return false;
    }//else
}//bool

bool playFrog(int n)
{
    if(gameCharacters[n]== 'F')
    {
        if(n>N-5)
        {
            if(gameCharacters[n-1]=='.')
            {
                //moving
                gameCharacters[n] = '.';
                gameCharacters[n-1]='F';
                
                for(int count=0; count<N; count++)
                {
                    cout << gameCharacters[count];
                }//for
                
                cout << endl;
                
                return true;
            }//if
            
            if(n>N-4)
            {
                if(gameCharacters[n-2]=='.')
                {
                    //moving
                    gameCharacters[n] = '.';
                    gameCharacters[n-2]='F';
                    
                    for(int count=0; count<N; count++)
                    {
                        cout << gameCharacters[count];
                    }//for
                    
                    cout << endl;
                    
                    return true;
                }//if
                
                else
                {
                    return false;
                }//else
                
            }//if
            
            else
            {
                return false;
            }//else
            
        }//if
        
        else
        {
            return false;
        }//else
    }//if
    
    else
    {
        return false;
    }//else
}//playfrog

void showMessage()
{
    cout << "Enter 0-5 for move, q to quit, r to restart game." << endl;
}//showMessage()

//Show the menu of possible operations
void startGame()
{
    cout << "Enter 0-5 for move, q to quit, r to restart game." << endl;
    
    cout << gameCharacters << endl;
    
    //to print out the moves in the array
    for(int count =0; count< N; count++)
    {
        cout << MOVES[count];
    }//for
    
    cout << endl;
    
}//startGame()


void playerMove()
{
    if (player == 1)
    {
        cout <<  "Enter move for Toad <<" << endl;
    }//if
    
    else
    {
        cout <<  "Enter move for Frog <<" << endl;
    }//else
}//playermove

void winner()
{
    if (gameCharacters[0] == '.' && gameCharacters[1]=='T' && gameCharacters[2]=='T' && gameCharacters[3]=='F' && gameCharacters[4]=='F' && gameCharacters[5]=='.')
    {
        
        if (player==2)
        {
            cout << "Toad Wins!" << endl;
            restartGame();
        }//if
        
        else
        {
            cout << "Frog Winds!" << endl;
            restartGame();
        }//else
        
    }//if
    
    else if (gameCharacters[0] == '.' && gameCharacters[1]=='.' && gameCharacters[2]=='T' && gameCharacters[3]=='T' && gameCharacters[4]=='F' && gameCharacters[5]=='F')
    {
        
        if (player==2)
        {
            cout << "Toad Wins!" << endl;
            restartGame();
        }//if
        
        else
        {
            cout << "Frog Winds!" << endl;
            restartGame();
        }//else
    }//if
    
    else if (gameCharacters[0] == '.' && gameCharacters[1]=='F' && gameCharacters[2]=='.' && gameCharacters[3]=='T' && gameCharacters[4]=='T' && gameCharacters[5]=='F')
    {
        
        if (player==2)
        {
            cout << "Toad Wins!" << endl;
            restartGame();
        }//if
        
        else
        {
            cout << "Frog Winds!" << endl;
            restartGame();
        }//else
    }//if
    
    else if (gameCharacters[0] == 'T' && gameCharacters[1]=='T' && gameCharacters[2]=='F' && gameCharacters[3]=='F' && gameCharacters[4]=='.' && gameCharacters[5]=='.')
    {
        
        if (player==2)
        {
            cout << "Toad Wins!" << endl;
            restartGame();
        }//if
        
        else
        {
            cout << "Frog Winds!" << endl;
            restartGame();
        }//else
    }//if
    
    
    
    
}//winner

void restartGame()
{
    cout << "Would you like to play again?" << endl;
    cout << "1: To replay" << endl;
    cout << "2: To exit" << endl;
    
    string again = "";
    
    do
    {
        getline(cin, again);
        
        if (again.length()>0)
        {
            switch (again[0])
            {
                case '1':
                {
                    
                    gameCharacters[0] = 'T';
                    gameCharacters[1] = 'T';
                    gameCharacters[2] = '.';
                    gameCharacters[3] = '.';
                    gameCharacters[4] = 'F';
                    gameCharacters[5] = 'F';
                    player=1;
                    
                    startGame();
                    
                }//case 1
                    break;
                    
                    
                case '2':
                {
                    exit(0);
                }//case 2
                    
                    break;
                    
                default:
                {
                    cout << "Invalid input" << endl;
                    cout << "1: To replay" << endl;
                    cout << "2: To exit" << endl;
                }//default
                    
            }//switch
            
        }//if
        
    }//do
    
    while (!(again=="1" || again=="2"));
    
}//restartGame()


One thought on “Toads and Frogs Game!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s