All pastes #2071450 Raw Edit

Untitled

public cpp v1 · immutable
#2071450 ·published 2011-05-29 03:08 UTC
rendered paste body
/* * simple 2-player tic-tac-toe solution. * this was developed by nDray, so any resemblance * to existing software is purely coincidential. * * feel free to change, use and share * *    e-mail: cyberdray@gmail.com * v1.0 date: July-07-2007 * */#include <iostream>using namespace std;// the matrix of the tic-tac-toe gameint matrix[3][3] = { 0 };   // set 1st value to zero, and other will be                            // set to zero as well, by being ignored/* * detect if any line, column or diagonal * were filled by the same player, given the * last move and the player * * since the comparisons implie only 3 values * i preferred to use simple if's instead of * "for" loops... it's ugly to read, but simple * to the machine * */bool won(int row, int column, int player){    //cout << "col: " << column << " row: " << row << " player: " << player << endl;    // check the row    if(matrix[row][0] == matrix[row][1] && matrix[row][1] == matrix[row][2])    {        //cout << "horizontal" << endl;        return true;    }        // check the column    if(matrix[0][column] == matrix[1][column] && matrix[1][column] == matrix[2][column])    {        //cout << "vertical" << endl;        return true;    }    // check "negative" diagonal    if(column == row) // then it could be in a negative    {        if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matrix[2][2])        {            //cout << "negative" << endl;            return true;        }    }                // check "positive" diagonal    if((column + row) == 2) // then it could be in a positive    {        if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matrix[2][0])        {            //cout << "positive" << endl;            return true;        }    }    return false;}/*  * player makes a move. squares are numbered from 1 to 9 * left to right, top to bottom. * useful information as i'm not going to implement any printing * function at this point */bool play(int player){    int move = 0;    int row = 0;    int column = 0;    do    {        do        {            cout << "player " << player << ": ";        }        while(!(cin >> move) || move < 1 || move > 9);        row    = (move - 1) / 3;    // 1, 2, 3 => 0;    4, 5, 6 => 1;   7, 8, 9 => 2;        column = (move - 1) % 3;    // 1, 4, 7 => 0;    2, 5, 7 => 1;   3, 6, 9 => 2;                                    // might the column be simpler....?    }    while(matrix[row][column]);    matrix[row][column] = player;        if(won(row, column, player))        return true;    return false;}/*  * this is very stupid, but it's only so that people can understand. * the purpose of the program was to work, only..... */void printTable(void){    int place = 0;        system("cls");    for(int row = 0; row < 3; ++row, cout << endl)    {        for(int col = 0; col < 3; ++col)        {            ++place;            switch (matrix[row][col])            {                case 0:                    cout << place;                    break;                case 1:                    cout << 'X';                    break;                case 2:                    cout << 'O';                    break;                default:                    cout << "WTF??";            }        }    }}int main(){    int moves;    int player;    bool end;    char opt;        moves = 0;    //player = 1; // unnecessary    end = false;    printTable();        while(moves < 9 && !end)    {        ++moves;        player = (moves % 2 == 0) + 1;            if(play(player))            end = true;            printTable();    }        if(end)    {        cout << "player " << player << " wins" << endl;    }    else    {        cout << "draw" << endl;    }        system("pause");    return 0;}