I am new to c++ and have some other minor programming experience currently taking a college class intro to c++, we started into ADT's last week and were posed with the problem of converting a TicTacToe game that we worked on previously into a program that utilizes classes to operate. I have organized my code like we were shown in class. I found an example online that was close to what I had available to work with and attempted structuring it like the example. the code currently will compile without error but doesn't draw the board or call the user. Any help would be appreciated again I'm completely new to c++ so I assume there are things not done efferently or that are bad practice I apologize ahead of time. Thanks
main.cpp
#include "tictactoe.h"
#include<iostream>
using namespace std;
int main(int argc, char** argv) {
int input;
do
{
tictactoe boardDisplay();
tictactoe user();
} while (input != 0);
return 0;
}
tictactoe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
class tictactoe
{
public:
tictactoe();
void boardDisplay();
void user();
void alreadyTaken();
void winState();
void declareWinner(char);
protected:
};
#endif
and then a separate cpp file for the class
tictactoe.cpp
#include "tictactoe.h"
#include<iostream>
using namespace std;
tictactoe::tictactoe()
{}
int input; //user input variable
int turn = 0; //gamer turn counter
char xo = ' '; // X O board input
char board[10] = {' ','1','2','3','4','5','6','7','8','9'}; //board contents
//using 10 elements to match squares
//with appropriate array elements.
//Note: Using 1 array is easier than 3!
void tictactoe::boardDisplay(){
cout << " " << board[1] << " " << "|" << " " << board[2] << " " << "|" << " " << board[3] << " " << endl; // draws the board
cout << "----------" << endl;
cout << " " << board[4] << " " << "|" << " " << board[5] << " " << "|" << " " << board[6] << " " << endl; // draws the board
cout << "----------" << endl;
cout << " " << board[7] << " " << "|" << " " << board[8] << " " << "|" << " " << board[9] << " " << endl;
cout << "Turn#: " << turn << ". Please choose a number, 1 - 9 (or 0 to quit): "; // prompts the user for a spot on the board
}
void tictactoe::user(){
turn++;
cin >> input;
cout << endl << endl;
if(turn % 2 == 0) //checks if turn is odd or even player 1 odd and player 2 even
xo = 'X';
else
xo = 'O';
switch (input)
{
case 1:
if ((board[1] == 'x')||(board[1] == 'X')||(board[1] == 'o')||(board[1] == 'O')) // decides if spot is already taken
{
alreadyTaken();
break;
}
board[1] = xo;
break;
case 2:
if ((board[2] == 'x')||(board[2] == 'X')||(board[2] == 'o')||(board[2] == 'O')) // same process for all cases
{
alreadyTaken();
break;
}
board[2] = xo;
break;
case 3:
if ((board[3] == 'x')||(board[3] == 'X')||(board[3] == 'o')||(board[3] == 'O'))
{
alreadyTaken();
break;
}
board[3] = xo;
break;
case 4:
if ((board[4] == 'x')||(board[4] == 'X')||(board[4] == 'o')||(board[4] == 'O'))
{
alreadyTaken();
break;
}
board[4] = xo;
break;
case 5: if ((board[5] == 'x')||(board[5] == 'X')||(board[5] == 'o')||(board[5] =='O'))
{
alreadyTaken();
break;
}
board[5] = xo;
break;
case 6:
if ((board[6] == 'x')||(board[6] == 'X')||(board[6] == 'o')||(board[6] =='O'))
{
alreadyTaken();
break;
}
board[6] = xo;
break;
case 7:
if ((board[7] == 'x')||(board[7] == 'X')||(board[7] == 'o')||(board[7] =='O'))
{
alreadyTaken();
break;
}
board[7] = xo;
break;
case 8:
if ((board[8] == 'x')||(board[8] == 'X')||(board[8] == 'o')||(board[8] =='O'))
{
alreadyTaken();
break;
}
board[8] = xo;
break;
case 9:
if ((board[9] == 'x')||(board[9] == 'X')||(board[9] == 'o')||(board[9] =='O'))
{
alreadyTaken();
break;
}
board[9] = xo;
break;
case 0:
break; //quit flag
default:
cout << "You did not enter a valid input" << endl;
}
winState();
}
void tictactoe::alreadyTaken(){
cout << "This spot is already taken, try again." << endl;
turn--;
}
void tictactoe::winState() {
if((board[1] == board[4]) && (board[4] == board[7])) //Win on Left Column
declareWinner(board[7]);
else if((board[2] == board[5]) && (board[5] == board[8])) //Win on Middle Column
declareWinner(board[8]);
else if((board[3] == board[6]) && (board[6] == board[9])) //Win on Right Column
declareWinner(board[9]);
else if((board[1] == board[2]) && (board[2] == board[3])) //Win on Top Row
declareWinner(board[3]);
else if((board[4] == board[5]) && (board[5] == board[6])) //Win on Middle Row
declareWinner(board[6]);
else if((board[7] == board[8]) && (board[8] == board[9])) //Win on Bottom row
declareWinner(board[9]);
else if((board[1] == board[5]) && (board[5] == board[9])) //Win On '\' Diagonal
declareWinner(board[1]);
else if((board[3] == board[5]) && (board[5] == board[7])){ //Win on '/' Diagonal
declareWinner(board[7]);
}
}
void tictactoe::declareWinner(char val) { //Displays the Winner
switch(val)
{
case 'X': cout << "X Wins!";
break;
case 'O': cout << "O Wins!";
break;
}
input = 0; // end game
}