Thread: My board game
View Single Post
  #8 (permalink)  
Old 12-02-2007, 02:43 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I use this code to check the winner. lx and ly are the last move of the
player, cell2win refer to how many rows to connect in order to win.
Code:
bool Game_Win(char bTurn, int lx, int ly,int cell2win, int block)
{
    int b = 0, n = 1, oTurn, tx, ty;
    bool run = true;
    // oTurn mean opposite turn
    if (bTurn == 'X') oTurn = 'O';
    else oTurn = 'X';

    run = true; tx = lx; ty = ly;
    do {
        tx++;
        if (tx >= 20) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    run = true; tx = lx; ty = ly;
    do {
        tx--;
        if (tx < 0) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    if (n == cell2win && b < block) return true;

    n = 1; b = 0;
    run = true; tx = lx; ty = ly;
    do {
        ty++;
        if (ty >= 20) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    run = true; tx = lx; ty = ly;
    do {
        ty--;
        if (ty < 0) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
            run = false;
        }
    } while(run);

    if (n == cell2win && b < block) return true;

    n = 1; b = 0;
    run = true; tx = lx; ty = ly;
    do {
        ty--;
        tx--;
        if (ty < 0 && tx < 0) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    run = true; tx = lx; ty = ly;
    do {
        ty++;
        tx++;
        if (ty >= 20 && tx >= 20) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    if (n == cell2win && b < block) return true;

    n = 1; b = 0;
    run = true; tx = lx; ty = ly;
    do {
        ty--;
        tx++;
        if (ty < 0 && tx >= 20) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    run = true; tx = lx; ty = ly;
    do {
        ty++;
        tx--;
        if (ty >= 20 && tx < 0) run = false;
        if (board[ty][tx] == bTurn) {
                n++;
        } else if (board[ty][tx] == oTurn) {
                b++;
                run = false;
        } else {
                run = false;
        }
    } while(run);

    if (n == cell2win && b < block) return true;

    return false;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to siLenTz For This Useful Post:
HelloWorld (12-02-2007)