All pastes #1567384 Raw Edit

Someone

public text v1 · immutable
#1567384 ·published 2009-09-15 19:33 UTC
rendered paste body
#include <QtGui>
#include "mywidget.h"


//constructor!
MyWidget::MyWidget()
{
	//Setup GUI objects
	
	QGridLayout *layout = new QGridLayout;
	signalMapper = new QSignalMapper(this);

	for(int i = 0; i < 9; i++){
		//setup this row on the board
		board[i] = 0;
		gameButton[i] = new QPushButton("");

		//Add button to layout
		layout->addWidget(gameButton[i],i / 3,i % 3);

		//setup signal mapper for this button
		connect(gameButton[i],SIGNAL(clicked()),signalMapper,SLOT(map()));
		signalMapper->setMapping(gameButton[i], i);
	}

	connect(signalMapper,SIGNAL(mapped(int)),this,SLOT(moveMade(int)));
	setLayout(layout);

	playerOneTurn = true;
}

//moveMade slot handler
void MyWidget::moveMade(int location)
{
	QString displayMessage("");	

	if(board[location] != 0){
		QTextStream(stdout) << "That spot has already been taken, try again\n";
		displayMessage = "That spot has already been taken, try again";
	} else {
		if(playerOneTurn){
			board[location] = 1;
			gameButton[location]->setText("X");
			playerOneTurn = false;
		} else {
			board[location] = -1;
			gameButton[location]->setText("O");
			playerOneTurn = true;
		}
	}
	
	switch(checkWinner()){
		case -1:
			reset();   //reset game
			displayMessage = "Cat has won the game :(";
			break;
		case 1:
			reset();   //reset game
			displayMessage = "Player one has won the game!";
			break;
		case 2:
			reset();   //reset game
			displayMessage = "Player two has won the game!";
			break;
	}

	if(displayMessage != "")
		QMessageBox::information(this,"Tic Tac Toe - Qt",displayMessage,"ok");
}


//returns 1 or 2 if a player one, -1 if cat won, and 0 if game is still in play
int MyWidget::checkWinner()
{
        int i;  //Ill be using this in a lot of loops
        int value; //used for adding rows, cols, etc

	int winner = -1;  // -1 for cat, if its not a cat game this will be changed

        //check to see if game is over (I.E. No moves left)
        for(i = 0; i < 9; i++){
                if(board[i] == 0){
                        winner = 0;   //Game is not over!  keep playing
                }
        }

        //check rows for winner
        for(i = 0; i < 9; i += 3){
                value = board[i] + board[i+1] + board[i+2];
                if(value == 3)
                        winner = 1; //player one won!

                if(value == -3)
                        winner = 2; //player two won!
        }

        //check cols for a winner
        for(i = 0; i <= 3; i++){
                value = board[i] + board[i+3] + board[i+6];
                if(value == 3)
                        winner = 1; //player one won!

                if(value == -3)
                        winner = 2; //player two won!
        }

        //check for wins not in cols or rows

        value = board[0] + board[4] + board[8];  //top left, middle, bottom right
        if(value == 3)
                winner = 1; //player one won!
        if(value == -3)
                winner = 2; //player two won!


        value = board[2] + board[4] + board[6];  //top right, middle, bottom left
        if(value == 3)
                winner = 1; //player one won!
        if(value == -3)
                winner = 2; //player two won!

        //return
        return winner;

}

//resets the game!
void MyWidget::reset()
{

	playerOneTurn = true;
	for(int i = 0; i < 9; i++){
		//reset all the values
		board[i] = 0;
		gameButton[i]->setText("");
	}

}