All pastes #1025098 Raw Edit

Untitled

public cpp v1 · immutable
#1025098 ·published 2008-05-21 18:22 UTC
rendered paste body
//Class function file#include "board.h"//constructorBoard::Board(){	playerOneTurn = true;  //set move to player one	for(int i = 0; i < 9; i++){    //create board		boardData[i] = 0;  	}}//print the board!void Board::printBoard(){	for(int i = 0; i < 9; i += 3){		cout << " " << outputMarker(i) << " | " << outputMarker(i+1) << " | " << outputMarker(i+2) << "\n";				//avoids printing a line at the end of the board		if(i != 6){			cout << "-----------\n";		}	}}//used by printBoardchar Board::outputMarker(int location){	int type = boardData[location];	char returnChar;	if(type == 1){		returnChar = 'X';   //X	} else if(type == -1) {		returnChar = 'O';   //O	} else {		switch(location + 1){  // No move is in the sector, get the number value of location.			case 1:				returnChar = '1';				break;			case 2:				returnChar = '2';				break;			case 3:				returnChar = '3';				break;			case 4:				returnChar = '4';				break;			case 5:				returnChar = '5';				break;			case 6:				returnChar = '6';				break;			case 7:				returnChar = '7';				break;			case 8:				returnChar = '8';				break;			case 9:				returnChar = '9';				break;			default:				returnChar = '-';		}		}	return returnChar;   //return results}//Function handler for making a move!bool Board::makeMove(int location){	if(!checkMove(location))		return false;	location--;   //translate the user interface numbers to array numbers		if(playerOneTurn){  		//If its player's one turn		playerOneTurn = false; 	//Make it player's two turn next time			boardData[location] = 1; //make the move for player one	} else {			 //has to be player two's turn		playerOneTurn = true;		boardData[location] = -1;  //Make the move for player two	}	return true;}//validates the move wanting to be madebool Board::checkMove(int location){	location--; //translate user input to array numbers	if(location < 0 || location > 8 || boardData[location] != 0)   //Checks to see if move is on board, and if the spot is empty		return false;	else		return true;}//gets player turnint Board::getTurn(){	if(playerOneTurn)		return 1; //1 for player one	if(!playerOneTurn)		return 2; //2 for player two}//checks to see if there is a winner, returns -1 for cat gameint Board::checkWinner(){	int i;  //Ill be using this in a lot of loops	int value; //used for adding rows, cols, etc	//check rows for winner	for(i = 0; i < 9; i += 3){		value = boardData[i] + boardData[i+1] + boardData[i+2];		if(value == 3)			return 1; //player one won!		if(value == -3)			return 2; //player two won!	}	//check cols for a winner	for(i = 0; i <= 3; i++){		value = boardData[i] + boardData[i+3] + boardData[i+6];		if(value == 3)			return 1; //player one won!		if(value == -3)			return 2; //player two won!	}	//check for wins not in cols or rows		value = boardData[0] + boardData[4] + boardData[8];  //top left, middle, bottom right	if(value == 3)		return 1; //player one won!	if(value == -3)		return 2; //player two won!		value = boardData[2] + boardData[4] + boardData[6];  //top right, middle, bottom left	if(value == 3)		return 1; //player one won!	if(value == -3)		return 2; //player two won!	//check to see if game is over (I.E. No moves left)	for(i = 0; i < 9; i++){		if(boardData[i] == 0){			return 0;   //Game is not over!  keep playing		}	}	//well, noone one, and game is over, call it cat	return -1;}