开发者

provide loop statement to force player game pieces to bottom row of graphical array

hi I have been building a C code for the game connect 4. the game will have single player vs easy ai/hard ai and finally 2 player mode. game board and column selection (where you drop your game piece) all work at the moment, I am just trying to get a while/if or for/if working to get the pieces to always start at the bottom of the game board and loop upwards in the matrix until it finds an empty spot in that column the user chose.

This is what I have now, I have tried several unsuccessful attempts at different methods, and now I believe I have a complete mash up of bad syntax. I am compiling in gcc.

#include <stdio.h>
#include <stdlib.h>

// function: check
// input: takes user [5][*]
// output: completes check to place the users '@' piece on board, if row 6 is taken it moves to row 5 and so on.

int space_1 (char b[][]) {
int k,x
while (b[][] == '@' || '#'){
    for (k=5;k>5;k--){
        if (b[k][x] == ' '){            
        board[k][x] = '@';
        }
    }
}
}
// function: check
// input: takes user [5][*]
// output: completes check to place the users '#' piece on board, if row 6 is taken it moves to row 5 and so on.

int space_2 (char b[][]) {
int k,x
while (b[][] == '@' || '#'){
    for (k=5;k>5;k--){
        if (b[k][x] == ' '){            
        board[k][x] = '#';
        }
    }
}
}
//function: display_board
// input : where to place piece on board
// output: places users piece on board

void display_board (char b[][6]) {
   printf ("\033[01;33m  1   2   3   4   5   6   7\n");
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[0][0], b[0][1], b[0][2], b[0][3], b[0][4], b[0][5], b[0][6]);
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[1][0], b[1][1], b[1][2], b[1][3], b[1][4], b[1][5], b[1][6]);
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[2][0], b[2][1], b[2][2], b[2][3], b[2][4], b[2][5], b[2][6]);
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[3][0], b[3][1], b[3][2], b[3][3], b[3][4], b[3][5], b[3][6]);
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[4][0], b[4][1], b[4][2], b[4][3], b[4][4], b[4][5], b[4][6]);
   printf ("\033[01;33m-----------------------------\n");
   printf ("\033[01;33m| %c | %c | %c | %c | %c | %c | %c |\n", b[5][0], b[5][1], b[5][2], b[5][3], b[5][4], b[5][5], b[5][6]);
   printf ("\033[01;33m-----------------------------\n");
}

 void display_board2 (char b[][6]) {
   int i, j;
    printf ("\033[01;33m  1   2   3   4   5   6   7\n");
    printf ("\033[01;33m-----------------------------\n");
    for (i=0; i<6; i++) {
          for (j=0; j<7; j++) {
         printf ("\033[01;33m| %c ", b[i][j]);
       }
           printf ("\033[01;33m|\n");
       printf ("\033[01;33m-----------------------------\n");
      }  
}
void main/*2*/ () {
        char board[5][6];
        int row, col, status;

// initialize the game board w/ blanks
board[0][0] = ' '; board[0][1] = ' '; board[0][2] = ' '; board[0][3] = ' '; board[0][4] = ' '; board[0][5] = ' '; board[0][6] = ' '; 
board[1][0] = ' '; board[1][1] = ' '; boar开发者_Python百科d[1][2] = ' '; board[1][3] = ' '; board[1][4] = ' '; board[1][5] = ' '; board[1][6] = ' '; 
board[2][0] = ' '; board[2][1] = ' '; board[2][2] = ' '; board[2][3] = ' '; board[2][4] = ' '; board[2][5] = ' '; board[2][6] = ' '; 
board[3][0] = ' '; board[3][1] = ' '; board[3][2] = ' '; board[3][3] = ' '; board[3][4] = ' '; board[3][5] = ' '; board[3][6] = ' '; 
board[4][0] = ' '; board[4][1] = ' '; board[4][2] = ' '; board[4][3] = ' '; boar d[4][4] = ' '; board[4][5] = ' '; board[4][6] = ' '; 
board[5][0] = ' '; board[5][1] = ' '; board[5][2] = ' '; board[5][3] = ' '; board[5][4] = ' '; board[5][5] = ' '; board[5][6] = ' ';


//display_board (board);
display_board2 (board);

while (1) {
    printf ("\033[22;31m@'s move enter column: ");
    scanf ("%d", &col);
    space_1[5][col-1] = '@';    // mark the board   
    display_board2 (board);

    // check for winner
    status = check2 (board);
    if (status == 1) {
        printf ("@ wins!\n");
        break;
    }

    printf ("\033[22;34m#'s move enter column: ");
    scanf ("%d", &col);
    space_2[5][col-1] = '#';  // mark the board
    display_board2 (board);


    // check for winner
    status = check2 (board);
    if (status == 1) {
        printf ("# wins!\n");
        break;
    }
}

    printf ("Thanks for playing!\n");
}

I have admited all additional sub routines to focus on my poor c programming skills any and all help is much appreciated.


int space_1 (char b[][]) {

You can only leave blank the leftmost array dimension when declaring array arguments.

int k,x
while (

You need a ; to terminate the declaration of variables. In C, statements are terminated by the inclusion of a ;.

while (b[][] == '@' || '#') {

This test is not testing what you think (assuming b[][] meant something). What this does is compare b[][] to '@' and, if different, compare '#' to 0 (zero). The last comparison is always false.

for (k=5; k>5; k--) {

k is never greater than 5. The body of this loop is never executed.

void main() {

Unless you're writing for a freestanding environment (a computer without an Operating System) the main function returns an int. It should be defined with int main(void) { /*...*/ }

Suggestion:

Keep your code compilable at all times. Do not add something if the code doesn't compile. When adding features, keep saving and test compiling.


Of course passing the b[][] can be circumvented by passing it as char **b

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜