1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
//This is a modified version of Connect Four, in which you can choose the number of connections
//Made by Andrea Roark, A01974967
#include <iostream>
#include <ctime>
/* Prompt the user for how many connections to win the game. The number of connections must
be 3, 4, 5, or 6. */
int getSize(){
int input;
std::cout<<"Welcome to Connect N! \nHow many connections would you like? \nPlease enter a number between 3 and 6.";
std::cin>>input;
std::cin.ignore();
if(input<3) //validating input. if they can't follow directions they don't get to make decisions
input=3;
if(input>6) //perhaps not the nicest option but you know
input=6;
return input;
}
/* Create a dynamic 2D character array for the game board */
//used this guy for reference http://aprogrammersday.blogspot.com/2010/09/char-array-of-strings-in-c-and-c-plus.html
char** buildBoard(int winSize){
/* Hint:
Step 1) char **GameBoard = new char *[WinSize+EXTRA_ROWS];
Step 2) use loop to allocate the char array for each of the rows.*/
char**gameBoard=new char*[winSize+1]; //double pointer of type char
for(int i=0; i<(winSize+2); i++){ //maximum number of rows
gameBoard[i]=new char[winSize+2]; //each index of the array of pointers is an array of chars
for(int j=0; j<(winSize+3); j++){ //max number of cols
gameBoard[i][j]=' '; //initializing each char in the array
}
}
return gameBoard;
}
/* Provide a pretty display of the game board */
void printBoard(char**gameBoard, int winSize){
//do some pretty stuff here
std::cout<<" C O N N E C T "<<winSize<<std::endl;
//index array label
int *index;
index=new int [winSize+3];
for(int i=0; i<=(winSize+3)-1; i++){
index[i]=i;
std::cout<<"| "<<index[i];
}
std::cout<<"|"<<std::endl;
//print gameBoard
for(int i=0; i<(winSize+2); i++){
for(int j=0; j<(winSize+3); j++){
std::cout<<"| "<<(gameBoard[i][j]);
}
std::cout<<"|"<<std::endl;
}
}
/* Ask the player for their move. Specifically, it asks the player for the column that the player
wishes to drop his piece into and sets the value of the lowest row of that column that is not occupied
by an 'x' or an 'o' to 'o'. */
void playerMove(char**&gameBoard, int winSize){
int colChoice;
bool done=false;
std::cout<<"Enter your column choice: "<<std::endl;
std::cin>>colChoice;
std::cin.ignore();
//if you can't enter the right number. . .
if(colChoice<0)
colChoice=0;
//then I'll pick it for you.
if(colChoice>winSize+3)
colChoice=winSize+3;
for(int i=winSize+1; i>=0; i--){
if(gameBoard[i][colChoice]==' '){
gameBoard[i][colChoice]='o';
break;
}
if(gameBoard[0][colChoice]!=' ') //checking to make sure if column has space.
std::cout<<"That column is full. "; playerMove(gameBoard, winSize); //If the top row is not empty, it is full
}
}
/* Randomly generate the computer move. Specifically, it uses rand() function to generate the
column that the computer wishes to drop his piece into and sets the value of the lowest row of that
column that is not occupied by an 'x' or an 'o' to 'x'. */
void compMove(char**gameBoard, int winSize){
int compChoice;
srand(static_cast<unsigned int>(time(NULL))); //initializing random number generator for computer's moves
compChoice=rand()%(winSize+3)-1;
for(int i=(winSize+2)-1; i>=0; i--){
if(gameBoard[i][compChoice]==' '){
gameBoard[i][compChoice]='x';
break;
}
}
}
/* Check for a victory by horizontally-laid pieces. If there are N game pieces in a continuous
horizontal line, the last player that placed pieces wins. */
bool checkRow(char**gameBoard, int winSize){
for(int i=0; i<=winSize; i++){
for(int j=0; j<=(winSize+3)-1; j++){
if(gameBoard[i][j]=='o'){
for(int k=0; k<=winSize; k++)
if(gameBoard[i][j+k]=='o'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
if(gameBoard[i][j]=='x'){
for(int k=0; k<=winSize; k++)
if(gameBoard[i][j+k]=='x'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
}
}
return false;
}
/* Check for a victory by vertically-laid pieces. If there are N game pieces in a continuous vertical
line, the last player that placed pieces wins. */
bool checkColumn(char**gameBoard, int winSize){
for(int i=0; i<winSize; i++){ //rows
for(int j=0; j<(winSize+3)-1; j++){ //columns
if(gameBoard[i][j]=='o'){
for(int k=0; k<winSize; k++){
if(gameBoard[i+k][j]=='o'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
}
if(gameBoard[i][j]=='x'){
for(int k=0; k<winSize; k++){
if(gameBoard[i+k][j]=='x'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
}
}
}
return false;
}
/* Check for a victory by diagonally-laid pieces, rising to the right. If there are N game pieces in
a continuous diagonal line, the last player that placed pieces wins. */
bool checkRightDiagonal(char**gameBoard, int winSize){
//checks for player win
for(int i=winSize; i>=0; i--)
for(int j=0; j<=(winSize+3)-1; j++)
if(gameBoard[i][j]=='o'){
for(int k=0; k<=winSize; ++k)
if(gameBoard[i-k][j-k]=='o'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
//checks for comp win
for(int i=(winSize+2)-1; i>=0; i--)
for(int j=0; j<=(winSize+3)-1; j++)
if(gameBoard[i][j]=='x'){
for(int k=0; k<=winSize; k++)
if(gameBoard[i-k][j-k]=='x'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
return false; //I guess nobody won
}
/* Check for a victory by diagonally-laid pieces, rising to the left. If there are N game pieces in a
continuous diagonal line, the last player that placed pieces wins. */
bool checkLeftDiagonal(char**gameBoard, int winSize){
//checks for player win
for(int i=(winSize+2)-1; i>=0; i--)
for(int j=(winSize+3)-1; j>=0; j--)
if(gameBoard[i][j]=='o'){
for(int k=0; k<=winSize; k++)
if(gameBoard[i-k][j-k]=='o'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
//checks for comp win
for(int i=(winSize+2)-1; i>=0; i--)
for(int j=(winSize+3)-1; j>=0; j--)
if(gameBoard[i][j]=='o'){
for(int k=0; k<=winSize; k++)
if(gameBoard[i-k][j-k]=='o'){
int count=0;
++count;
if(count==winSize)
return true;
}
}
return false;
}
/* Check for a tie. If all columns are filled and there are not any N game pieces in a continuous
horizontal, vertical, and diagnoal line, no player is the winner. */
bool checkTie(char**gameBoard, int winSize){
//keeps count of moves made
static int moves=0;
++moves;
//if moves equal total slots, game is tie
if(moves==(winSize+2)*(winSize+3))
return true;
else return false;
}
/* Clean up the game board. That is, deallocate the game board from memory */
void deleteBoard(char**gameBoard, int winSize){
for(int i=0; i<=winSize+2; i++){
delete[] gameBoard[i];
gameBoard[i]=NULL;
}
delete[] gameBoard;
gameBoard=NULL;
return;
}
|