I am doing a program similar to connect four but instead of lining of four pieces, the winner has to spell out my last name (dumoulin) to win. It can be won in any direction, up, down, left, right, or any diagonal direction. I have the program running but have no idea how to create a check winner function to see who won. It is supposed to return true if victory has been achieved and false otherwise. Here is what I have so far.
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
void DrawBoard(char []);
bool CheckWinner(int x, int y, char board[][8]);
int main ()
{
//CONNECT ME GAME
/*
1. Create/Initialize Board
2. Get Moves
3. See if Winner
*/
char board[144];
int col;
int row;
char letter;
int turn;
cout << " CONNECT ME" << endl;
cout << "Directions:" << endl;
cout << "This is a two player game that is very similar to connect four. This is a game " << endl;
cout << "where two players alternate turns. On a player's turn, the said player will" << endl;
cout << "choose a letter to drop in one of the chutes (numbered 1 -12) along the top of" << endl;
cout << "the board. The letter will drop into the space avaialable in that chute until" << endl;
cout << "the chute is filled. A player wins by dropping a letter that completes the name" << endl;
cout << "dumoulin in any direction. The player that plays the letter that completes the" << endl;
cout << "name is the winner of the game. If the board becomes full without the name" << endl;
cout << "dumoulin being spelled out, the game will end in a tie." << endl;
cout << endl;
//Initialize board with spaces
for(int i = 0; i < 144; i++)
{
board[i] = ' ';
}
int boardSpot;
char winner;
for(int index = 0; index < 144; index++)
{
DrawBoard(board); // Draws board for players to see
while(!bMoveValid) // While bMoveValid = true
{
if(turn == 0) // Enter move for First Player
{
cout << "Player 1, choose a column to drop a letter in: ";
cin >> col;
cout << "Choose a letter to drop (d,u,m,o,u,l,i,n): ";
cin >> letter;
}
else // Code for Second Player
{
cout << "Player 2, choose a column to drop a letter in: ";
cin >> col;
cout << "Choose a letter to drop (d,u,m,o,u,l,i,n): ";
cin >> letter;
}
//Process (validate) move
// a - Col between 1 and 12 inclusive
// b - Spot not taken and not at to of row
// c - Rows start at bottom and work way up
You just need to check each horizontal, vertical and diagonal for if they spell your name. You can adapt the general formula I posted here: http://www.cplusplus.com/forum/general/118587/ for your purposes. You just need to evaluate a lot more squares and increment through a char array (which should have your name stored in) to see if the entire array is matched, rather than just simply looking for 'X' or 'O'.
A small note too. Since your name is so long and that grid is so small, all games will end up being drawn unless one player plays very very very badly...