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

  • Thread starter charmedbeauty
  • Start date
  • Tags
    2d Array
In summary, the conversation is about the author trying to flip tokens in their reverse code but they are stuck and unsure how to implement a function that checks directions. They provide their code and mention that they are using a function called "playingame()" to switch turns in the game. They also mention a function called "changetoken()" that is used to change the tokens on the board.
  • #1
charmedbeauty
271
0

Homework Statement



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.



Homework Equations





The Attempt at a Solution



Code:
/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.
 
Physics news on Phys.org
  • #2




Thank you for your post. It seems like you are trying to implement the flipping mechanic in your reverse code, but are stuck on how to do so. I suggest breaking down the problem into smaller steps and tackling them one at a time. Here are some suggestions for implementing the flipping function:

1. Start by defining the function, let's call it "flip_token", and specify its parameters. Since you want to check the token in all directions, the function should take in the current position of the token (i,j) and the current turn as parameters.

2. Next, you can use conditional statements to check if the current token is black or white. If it is black, you can use a for loop to check all the tokens in each direction until you reach a white token. Once a white token is reached, you can flip all the tokens in between. The same logic can be applied for the white token as well.

3. In order to check the tokens in each direction, you can use a nested for loop that iterates through the array in all 8 directions (up, down, left, right, and the 4 diagonals).

4. As you mentioned, your function should continue flipping until it reaches a token of the same color. To do so, you can use a while loop that checks if the next token is of the same color. If it is, the loop can terminate.

5. Finally, you can call this function in your main code whenever a token is placed on the board.

I hope this helps guide you in implementing the flipping function in your reverse code. Good luck!
 

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

1. What is the purpose of flipping tokens in 2d array Othello/Reversi?

The purpose of flipping tokens in Othello/Reversi is to capture your opponent's tokens and change them to your own color. This allows you to gain control of more spaces on the game board and potentially win the game.

2. How do you flip tokens in a 2d array Othello/Reversi game?

Tokens can be flipped in Othello/Reversi by placing your token next to your opponent's token in a straight line, then flipping all the tokens in that line to your own color. The line can be horizontal, vertical, or diagonal.

3. Can tokens be flipped more than once in a single turn?

No, tokens can only be flipped once per turn in a 2d array Othello/Reversi game. This means that each move can only flip tokens in one direction. However, one move can result in multiple token flips if it creates a chain reaction.

4. Is there a specific strategy for flipping tokens in Othello/Reversi?

Yes, there are various strategies for flipping tokens in Othello/Reversi. One strategy is to try to gain control of the corners and edges of the game board, as these spaces are harder for your opponent to flip. It is also important to think ahead and anticipate your opponent's moves to set up potential token flips.

5. What happens when all spaces on the game board are filled and there are still tokens to be flipped?

In this scenario, the player with the most tokens on the board wins the game. If both players have the same number of tokens, the game ends in a tie. It is important to keep track of the number of tokens on the board and strategize accordingly to avoid a tie game.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
763
  • Engineering and Comp Sci Homework Help
Replies
4
Views
940
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
899
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top