Hi i have created a game of noughts and crosses in c++ I am new to this so the code is a bit messy, but i would love to get any feedback from you lot about the actual game and not the code for now.
main functions that i have added is playable music from the beginning which can be turned off in the menu and also the option to change the colours of the background and the text from the menu.
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <MMSystem.h>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int rear;
char player = 'X';
void mainMenu();
void play();
void music();
void colour();
//Strings for the Player Names to be Chosen//
string charName = "Player1";
string charName2 = "Player 2";
//Noughts and Crosses Home Screen//
int main()
{
PlaySound(TEXT("Tetris.wav"), NULL, SND_ASYNC);
char userChoice = 'o';
cout << "\n\n\n\n\n\n\n\n\n\n\n WELCOME TO NOUGHTS AND CROSSES" << endl
<< "\n\n Press ENTER to continue ";
cin.ignore();
mainMenu();
return 0;
}
//Main Menu With Options//
void mainMenu()
{
system("CLS");
char userChoice = 'o';
do
{
//Main Menu//
cout << "\n\n\n Welcome to Noughts and Crosses" << endl
<< "\n\n 1. Play Game " << endl
<< " 2. How to Play" << endl
<< " 3. Music"<< endl
<< " 4. Change Colours" << endl
<< " 5. Game Details" << endl
<< " 6. Credits" << endl
<< " 7. Exit" << endl;
cout << "\n Please make a choice: ";
cin >> userChoice;
system("CLS");
if (userChoice == '1')
play();
else if (userChoice == '2')
cout <<
"\n\n\n Instructions:\n\n Enter the player names then press enter to proceed with the game.\n To select the square you would like to place your marker enter the square name followed by the ENTER key.\n Repeat this until a player has three of their markers in a row. This can be in any direction.\n\n Enjoy.\n\n\n"
<< endl;
else if (userChoice == '3')
music();
else if (userChoice == '4')
colour();
else if (userChoice == '5')
cout << "\n\n\n Made in Visual Studios using C++\n Error code returned as = 0x80070002\n Successful Code Return Game Played = 1073741510 (0xc000013a).\n Version 2.0\n\n" << endl;
else if (userChoice == '6')
cout << "\n\n\n Created by Jamie Clifford.\n\n " << endl;
else if (userChoice == '7')
{
cout << "\n\n\n Thank you for playing, Good bye" << endl;
}
else
cout << "\n\n\n Error - Please choose again" << endl;
} while (userChoice != '7');
}
//Background Music Option to Play or Turn Off//
void music()
{
char userChoice = 'o';
cout << " \n\n\n Would you like to turn music ON or OFF?\n\n" << endl
<< " 1. ON" << endl
<< " 2. OFF\n";
cout << "\n Please make a choice: ";
cin >> userChoice;
system("CLS");
if (userChoice == '1')
PlaySound(TEXT("Tetris.wav"), NULL, SND_ASYNC);
else if (userChoice == '2')
PlaySound(NULL, NULL, SND_ASYNC);
}
//Change the Background and Text Colours//
void colour()
{
char userChoice = 'o';
cout << "\n\n\n What Colour Background and Text Would You Like?\n\n" << endl
<< " 1. Black Background and White Text" << endl
<< " 2. Grey Background and Black Text" << endl
<< " 3. Green Background and Light Yellow Text" << endl
<< " 4. Blue Background and Light Aqua Text" << endl
<< " 5. Red Background and Green Text" << endl;
cout << "\n Please make a choice: ";
cin >> userChoice;
system("CLS");
if (userChoice == '1')
system("Color 07"); //Black Background and White Text//
else if (userChoice == '2')
system("Color 70"); //Grey Background and Black Text//
else if (userChoice == '3')
system("Color 2E"); //Green Background and Yellow Text//
else if (userChoice == '4')
system("Color 1B"); //Blue Background and Light Aqua Text//
else if (userChoice == '5')
system("Color 4A"); //Red Background and Green Text//
}
//Board//
void Draw()
{
system("CLS");
//Board//
cout << "\n\n\n Noughts and Crosses \n\n";
for (int i = 0; i < 3; i++)
{
cout << " ";
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
//Player Input//
void Input()
{
int a;
rear++;
player;
do
{
//Beginning of game, where player input their chosen space//
{ if (player == 'X')
cout << "\n It's " << charName << "'s turn. Enter a number: ";
else if (player == 'O')
cout << "\n It's " << charName2 << "'s turn. Enter a number: ";
}
cin >> a;
if (a == 1 && matrix[0][0] == '1')
matrix[0][0] = player;
else if (a == 2 && matrix[0][1] == '2')
matrix[0][1] = player;
else if (a == 3 && matrix[0][2] == '3')
matrix[0][2] = player;
else if (a == 4 && matrix[1][0] == '4')
matrix[1][0] = player;
else if (a == 5 && matrix[1][1] == '5')
matrix[1][1] = player;
else if (a == 6 && matrix[1][2] == '6')
matrix[1][2] = player;
else if (a == 7 && matrix[2][0] == '7')
matrix[2][0] = player;
else if (a == 8 && matrix[2][1] == '8')
matrix[2][1] = player;
else if (a == 9 && matrix[2][2] == '9')
matrix[2][2] = player;
else {
cout << "\n Invalid number, please try again.\n\n ";
system("pause");
rear--;
cin.ignore();
cin.get();
a = -1;
}
} while (a == -1);
}
//Function to Switch Players//
void togglePlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
}
//Checks for a Winner//
char Win()
{
//first player//
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';
//second player//
if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';
return '/';
}
//Main Function to Play the Game//
void play()
{//Players enter their names//
cout << "\n\n\n Player 1 enter your name: ";
cin >> charName;
cout << "\n\n\n Player 2 enter your name: ";
cin >> charName2;
char choice;
Draw();
Start:
while (1)
{
Input();
Draw();
if (Win() == 'X')
{
cout << "\n " << charName << " Wins The Game ";
break;
}
else if (Win() == 'O')
{
cout << "\n " << charName2 << " Wins The Game " << endl;
break;
}
else if ( rear == 9)
{
cout << " Draw" << endl;
break;
}
togglePlayer();
}
//Choice to play the game again or return to the main menu//
cout << "\n\n ";
cout << " Do you want to go play again? ";
cout << "\n\n 1. Yes\n";
cout << " 2. No\n\n";
cout << " ";
cin >> choice;
if (choice == '1')
cout << "\n Enjoy\n";
else if (choice == '2')
mainMenu();
while (choice != '1');
{ for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = i * 3 + (j + 1) + 48;
}
}
rear = 0;
player = 'X';
goto Start;
}
}
Thank you in advance.