All pastes #229939 Raw Edit

Mine

public text v1 · immutable
#229939 ·published 2006-10-31 00:54 UTC
rendered paste body
#include "ConnectFour.h"

ConnectFour::ConnectFour()
{
	CurrentPlayer = BLACK;
	for(int i = 0; i < 7; i++)
	{
		Board.push_back(column());
	}
}
bool ConnectFour::move(int col)
{
	if(Board[col].size() == 6)
	{
		return false;
	}
	Board[col].push_back(currentplayer());
	if(CurrentPlayer == BLACK)
	{
		CurrentPlayer = WHITE;
	}
	else
	{
		CurrentPlayer = BLACK;
	}
	system("cls");
	return true;
}
piece ConnectFour::get(int col, int row)
{
	if(Board.size() > col && Board[col].size() > row)
	{
		return Board[col][row];
	}
	return EMPTY;
}
piece ConnectFour::winner()
{
	for(int i = 0; i < 7; i++)
	{
		for(unsigned int j = 0; j < Board[i].size(); j++)
		{
			if(get(i,j) != EMPTY &&
				(i < 4
				&& get(i, j) == get(i + 1, j)
				&& get(i, j) == get(i + 2, j)
				&& get(i, j) == get(i + 3, j))
				||
				(j < 3
				&& get(i, j) == get(i, j + 1)
				&& get(i, j) == get(i, j + 2)
				&& get(i, j) == get(i, j + 3))
				||
				(j < 3 && i < 4
				&& get(i, j) == get(i + 1, j + 1)
				&& get(i, j) == get(i + 2, j + 2)
				&& get(i, j) == get(i + 3, j + 3))
				||
				(j > 2 && i < 4
				&& get(i, j) == get(i + 1, j - 1)
				&& get(i, j) == get(i + 2, j - 2)
				&& get(i, j) == get(i + 3, j - 3)))
			{
				return Board[i][j];
			}
		}
	}
	return EMPTY;
}
piece ConnectFour::currentplayer()
{
	return CurrentPlayer;
}
bool ConnectFour::full()
{
	for(int i = 0; i < 7; i++)
	{
		if(Board[i].size() != 6)
		{
			return false;
		}
	}
	return true;
}










#include <iostream>
#include <string>
#include "connectfour.h"

//prototypes
void DisplayBoard(ConnectFour game);
char GetInput();
string Player(ConnectFour game);
string Player(piece p);
void main()
{
	char userChoice;
	ConnectFour game;
	piece winner;
	do
	{
		DisplayBoard(game);
		do
		{
			cout << endl << "Choose a column, " << Player(game) << ": ";
			userChoice = GetInput();
		}
		while(!game.move(userChoice - '0'));
		winner = game.winner();
		
	}
	while(winner == EMPTY && !game.full());
	DisplayBoard(game);
	if(!winner)
	{
		cout << "It's a tie!" << endl;
	}
	else
	{
		cout << Player(winner) << " wins!" << endl;
	}
}
string Player(ConnectFour game)
{
	if(game.currentplayer() == BLACK)
	{
		return "Black";
	}
	return "White";
}
string Player(piece p)
{
	if(p == BLACK)
	{
		return "Black";
	}
	return "White";
}
void DisplayBoard(ConnectFour game)
{
	char pieceChar;
	cout << " 0   1   2   3   4   5   6" << endl;
	for(int i = 5; i >= 0; i--)
	{
		for(int j = 0; j < 7; j++)
		{
			switch(game.get(j,i))
			{
				case 0:
					pieceChar = ' ';
					break;
				case 1:
					pieceChar = 'B';
					break;
				case 2:
					pieceChar = 'W';
					break;
			}
			cout << "["<< pieceChar << "] ";
		}
		cout << endl;
	}
}
char GetInput()
{
	string userChoice;
	do
	{
		cin >> userChoice;
		if(userChoice == "-1")
		{
			exit(0);
		}
	}
	while(userChoice != "0"	&& userChoice != "1"
		&& userChoice != "2" && userChoice != "3"
		&& userChoice != "4" && userChoice != "5"
		&& userChoice != "6");
	return userChoice[0];
}