1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with flipping tokens in 2d array othello/reversi.

  1. Oct 12, 2012 #1
    1. The problem statement, all variables and given/known data

    I am trying to flip tokens in my reverse code and I'm a little stuck

    so far I have a function that checks directions but I don't know how to implement it

    my code is below.



    2. Relevant equations



    3. The attempt at a solution

    Code (Text):

    /reversi.c
    //Samuel Gilbert
    //Date Due:12/10
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX_SIZE 8
    #define INVALID_MOVES {[a][1],[h][8],[h][1],[a][8]}
    #define WHITE 1
    #define BLACK 2
    #define CROSS 3
    #define EMPTY 0
    #define END_GAME 4

    void arraytwo(int arraytwo[][MAX_SIZE], int array[][MAX_SIZE]);
    void changetoken(int arraytwo[][MAX_SIZE], int turn, int countjPlus,//
    int countiPlus, int countiMinus, int countjMinus);
    int nexturn(int array[][MAX_SIZE], int turn);
    void updateboard(int array[][MAX_SIZE]);
    int playingame(int array[][MAX_SIZE], int turn);
    int main(){

    //initial board
    int array[MAX_SIZE][MAX_SIZE];
    int i,j;
    int turn = BLACK;

       printf("  abcdefgh\n");
       printf(" ----------\n");

       for(i=0;i<MAX_SIZE;i++){
           putchar(i+'1');
           putchar('|');

           for(j=0;j<MAX_SIZE;j++){

             if((i==0 && j==0) || (i==0 && j==7) || (i==7 && j==0) || (i==7 && j==7)){
                printf("X");
                array[i][j] = CROSS;
              }
                else if((i==3 && j==3) || (i==4 &&j==4)){
                   printf("w");
                   array[i][j] = WHITE;

      }  else if((i==3 && j==4) || (i==4 && j==3)){
                    printf("b");
                    array[i][j] = BLACK;
              }  else {
                array[i][j] = EMPTY;
                printf(".");
             }

          }
        putchar('|');
        printf("\n");
       }
        printf(" ----------\n");
       int k = 0;
       while (k < 100) {
         turn =  playingame(array, turn);
       }
       return 0;
    }
    int playingame(int array[][MAX_SIZE],int turn){
      // char input[2];

      int i=0;
       int j=0;
       if((turn == 2)){

          printf("it is Black's turn to move\n");
       }

       else{

          printf("whites turn\n");
       }

       char letter=0;
       int number=0;
       printf("Enter your move: \n");

       letter = getchar();
       number = getchar();
       getchar();

      j = letter -'a';
       i = number - '0'-1;

       //int counti = i;
       //int countj = j;


       if((array[i][j] == CROSS && turn == 2)){
          printf("error\n");
          turn = 2;
       }
       else if((array[i][j] == CROSS && turn == 1)){
          printf("error\n");
          turn = 1;
       }
       if((array[i][j] == EMPTY && turn == 2 )){

          array[i][j] = BLACK;
          updateboard(array);
          turn = nexturn(array, turn);
       }

    else if((array[i][j] == EMPTY && turn == 1)){

          array[i][j] = WHITE;
          updateboard(array);
          turn = nexturn(array, turn);
       }




       return turn;
       }

    void updateboard(int array[][MAX_SIZE]){

       printf("  abcdefgh\n");
       printf(" ----------\n");
       int i,j;
       for(i=0;i<MAX_SIZE;i++){

          putchar(i+'1');

    putchar('|');


          for(j=0;j<MAX_SIZE;j++){

             if(array[i][j] == CROSS){
                printf("X");

             }
             else if((array[i][j] == WHITE)){
                printf("w");
             }

             else if((array[i][j] == BLACK)){
                printf("b");
             }

             else {
                array[i][j] = EMPTY;
                printf(".");
             }

         }
          putchar('|');
          printf("\n");
       }
       printf(" ----------\n");

    }

    int nexturn(int array[][MAX_SIZE], int turn){


       if(turn == 2){
          turn = 1;
       }

       else{
          turn = 2;
       }
       printf("%d\n", turn);
       return turn;

    playingame(array, turn);
       }

    void arraytwo(int arraytwo[][MAX_SIZE], int array[][MAX_SIZE]){

       //int arraytwo[MAX_SIZE][MAX_SIZE];
       int i,j;
       for(i=0;i<MAX_SIZE;i++){
          for(j=0;j<MAX_SIZE;j++){

             arraytwo[i][j] = array[i][j];

           }
        }
        printf("%d\n", arraytwo[i][j]);
    }
    void changetoken(int array[][MAX_SIZE],int x2, int y2, int x1, int y1){

        int x,y,i;

        int changeboard = array[x1][y1];

       //down

        if(y1==y2 && x1>x2){

           for(x=0;x<size;x++){
              for(y=0;y<size;y++){

                 if(y==y1 && x>x2 && x<x1){

                    array[x][y] = changeboard;
                 }
              }
           }
       }

       //up

       if(y1==y2 && x2>x1){

          for(x=0;x<MAX_SIZE;x++){

            for(y=0;y<MAX_SIZE;y++){
                if(y==y1 && x<x2 && x>x1){
                   board[x][y]=changeboard;
                }
             }
          }
       }

       //left

       if(x1==x2 && y1>y2){

          for(x=0;x<size;x++){
             for(y=0;y<size;y++){

                if(x==x1 && y>y2 && y<y1){
                  board[x][y]=changeboard;

                }
             }

      }


       //right

       if(x1==x2 && y2>y1){

          for(x=0;x<size;x++){
             for(y=0;y<size;y++){

                if(x==x1 && y>y1 && y<y2){
                   board[x][y]=changeboard;

                }
             }
          }
       }

       //left up

      if(x1>x2 && y1>y2){
            for(i=1;x2+i<x1;i++){
                board[x2+i][y2+i]=changeboard;

            }
       }

       //right down


       if(x2>x1 && y2>y1){
          for(i=1;x1+i<x2;i++){
             board[x1+i][y1+i]=changeboard;

          }
       }

       //left down


       if(x2>x1 && y1>y2){
    for(i=1;x1+i<x2;i++){
             board[x2-i][y2+i]=changeboard;

          }
       }


       //right up


       if(x1>x2 && y2>y1){
          for(i=1;x2+i<x1;i++){
             board[x1-i][y1+i]=changeboard;

          }
       }
    }



    void checkmoveinsidearray(int turn,int number,int letter, int array[][MAX_SIZE])

    {

       if(array[i][j] == EMPTY){
          return 0;
       }

       if(letter > 7 || letter<0 || number>7 || number<0){

          return 0;
       }

    }

    int flipifneeded(){

       int i,j;


       if(turn==2 && array[i][j]== WHITE){
         i = i + deltax;
          j = j + deltay;

        else if(turn ==1 && array[i][j] == BLACK){

           i = i + deltax;
           j = j +deltay;

     

    I just need to implement a function that checks to see if the current token is black or white then checks in all directions to see if the token beside it is of opposite colour and flips until the same colour token is seen.

    for a more detailed info of the game see

    http://en.wikipedia.org/wiki/Reversi

    Thanks for any help.
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Help with flipping tokens in 2d array othello/reversi.
  1. Concatenating 2D array (Replies: 1)

Loading...