My dynamically allocated 2d array print random gibberish with no meaning - c

This code can get the first row to print, but for some reason will not get the first column to print. If this clarifies the question a bit the x values print but the y value barely does, I get some weird characters instead.
char** GenerateLand(int width, int height) {
char** GenLand;
int i;
GenLand = malloc(width * sizeof(char*));
for(i = 0; i < width; i++) {
GenLand[i] = malloc(sizeof(char) * height);
}
char RandChar = PickLandType();
int x, y;
for(x = 0; x < width; x++) {
if(x == 0) {
GenLand[x][0] = RandChar;
} else {
char RandChar1 = GenerateNeighbor(RandChar);
GenLand[x-1][0] = GenerateNeighbor(RandChar1);
for( y = 0; y < height ; y++){
if(y == 0){
GenLand[x][y] = RandChar;
} else {
char RandChar2 = GenerateNeighbor(RandChar);
GenLand[0][y-1] = GenerateNeighbor(RandChar2);
}
}
}
return GenLand;
for(i = 0; i < width; i++) {
free(GenLand[i]);
}
free(GenLand);
}
char GenerateNeighbor(char item)
{
switch(item)
{
case 'R':
return NewFromRuralLand();
break;
case 'F':
return NewFromForest();
break;
case 'T':
return NewFromTown();
break;
case 'W':
return NewFromWater();
break;
case 'C':
return NewFromCity();
break;
case 'M':
return NewFromMountain();
break;
default:
printf("Error!");
break;
}
}
char PickLandType()
{
int typeOfLand = rand()%5;
if(typeOfLand == 0){
return 'R';
} else if(typeOfLand == 1){
return 'F';
} else if(typeOfLand == 2){
return 'T';
} else if(typeOfLand == 3){
return 'W';
} else if(typeOfLand == 4){
return 'C';
} else if(typeOfLand == 5){
return 'M';
}
}
void PrintLand(char** Land, int width, int height) {
int x, y;
for( x = 0; x < width; x++){
for( y = 1; y < height ; y++){
printf("%c", Land[x][y]);
}
}
}
https://i.stack.imgur.com/t6Fec.png ^ Picture of error, was hoping someone could tell me why I keep getting these cryptic unreadable values. Sorry I don't have enough reputation points to directly embed the picture. I checked my NewFrom() functions and they all work so I do not believe they are the issue. The output is supposed to be organized by row and column:

I don't have enough reputation to comment, hence I have to write this as an answer.
Your freeing code at the end of GenerateLand will never run, there is an unconditional return statement prior to it. (NB: You shouldn't be freeing it regardless as you want to return GenLand, just remove the freeing code)
Without giving us the PickLandType and GenerateNeighbor functions, we can't tell you why your code returns a garbled mess of letters. Please edit and attach these functions.
This is the exact same question you asked before, except now you've attached different output - have you changed the code inbetween? If so, please attach the updated code.
This is bad code, the first iteration will access GenLand[0][-1]:
for(y = 0; y < height; y++) {
GenLand[0][y-1] = GenerateNeighbor(RandChar1);
}
Please try and keep your code nicely formatted to that it's easier for everyone to read.
If you want an answer that can actually help, give the GenerateNeighbor and PickLandType functions, and also give us an expected output.
Edit:
As Paul Ogilvie pointed out, the code in point 4 will also only ever generate the 0th column (the left most column). You probably meant for this to be GenLand[x][y] (but once again, see point 4)

Related

Error C2360 Initialization of "arr2" is skipped by "case" tag

I am new to programming.
This is a C language program.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdbool.h>
#define ture 1
#define false 0
void add(int m, int* arr,int n)
{
if (n == 32) return;
arr[n] += m;
if ( arr[n] > 1)
{
arr[n] = 0;
add(m, arr, ++n);
}
return;
}
int main(void)
{
int T,n,r,m,i,j,k;
bool check = ture;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &r);
switch (r)
{
case 10:
printf("%d", n);
break;
case 2:
int arr2[32] = { 0 };
if (n > 0)
{
for (i = 0; i < 32 ; i++)
{
arr2[i] = n % 2;
n = n / 2;
}
for (j = 31; j >= 0; j--)
{
if (arr2[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr2[j]);
}
}
}
else if (n == 0)printf("%d", 0);
else if (n < 0)
{
n = -n;
for (i = 0; i < 32; i++)
{
arr2[i] = n % 2;
n = n / 2;
}
for (k = 0; k < 32; k++)
{
arr2[k] = !arr2[k];
}
add(1, arr2, 0);
for (j = 31; j >= 0; j--)
{
if (arr2[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr2[j]);
}
}
break;
}
case 8:
int arr8[11] = { 0 };
if (n > 0)
{
for (i = 0; i < 11; i++)
{
arr8[i] = n % 8;
n = n / 8;
}
for (j = 10; j >= 0; j--)
{
if (arr8[j] == 0 && check == ture) continue;
else
{
check = false;
printf("%d", arr8[j]);
}
}
}
}
}
return 0;
}
When I run the program in VS2022.There is a bug.
Error C2360 Initialization of "arr2" is skipped by "case" tag Project5 C:\code\C\C_Single\Project5\Project5\test.cpp 74
I don't understand why this is happening.
In my opinion,when I select the contents of case8, I don't need the contents of case2, certainly,including the declaration of arr2.But obviously the compiler doesn't think that way.
So I turn to google for help.
However,google tells me something like this.
Your search - Error C2360 Initialization of "arr2" is skipped by "case" tag - did not match any documents.
Suggestions:
Make sure that all words are spelled correctly.
Try different keywords.
Try more general keywords.
Try fewer keywords.
So I want to get help in stackoverflow.Can anyone help me?
This is one reason that goto statements are frowned upon in modern code.
A case label is not much more that a regular label, and the switch will do something like:
if(value==2) goto label2;
if(value==3) goto label3;
etc.
But when you declare an array like:
int arr[10];
or actually any variable that goes on the stack, the compiler needs to increase the stack pointer to make space for that. in this case:
sp += 10 * sizeof(int)
(Of course this depends on your system/compiler etc)
So what happens if you put this piece of code, in between to (case) labels...
label2:
//int arr[10];
sp += 10*sizeof(int);
label3:
...
// end of scope for arr
sp -= 10*sizeof(int);
// or not?
Yeah it happens only half the time. So now you end up at the end of you switch statement, and your compiler doesn't know weather to decrease the stack pointer or not.
The compiler warns you that the initialization of the array arr2 can be skipped if the control will be passed to the label case 8:. In this case the array will have indeterminate values.
To avoid the compiler message just enclose statements after labels in braces creating a compound statement like for example
case 2:
{
int arr2[32] = { 0 };
//...
}
You have several problems here, within your switch(). (I'm just going to focus on that.)
Firstly, declaring variables within case clauses is problematic: providing them with initialisers, even more so. If you enclose the entire case clause within curly braces, that's a lot better. You also constrain the scope of your case-dependent variables to within that specific case.
Secondly, you have a significant logic error in your switch() statement. Your case 2: clause only hits a break in the n < 0 instance: in all others, it will fall through to case 8:. This is very clearly incorrect.
Thirdly, your case 8: clause has no break statement. As it's the last in the switch(), that's benign - you'll "fall out the bottom", but it's bad practice.
Finally, there is no default: clause. In just about every situation you use a switch() you want to catch the default: case, as it's either going to need a default handling, or it indicates an error condition.
In summary: brace your case clause code, so you can do as you with with, and scope, the variables you declare, and be rigorous about break and default: use. You'll thank me in the future!

Snake Game implementation in C

I need some help with some homework. I am supposed to make a Snake Game by using mostly "basic" c, only simple concepts.
To explain my thought process: Controls are the usual WASD. The grid we re playing in is an array. Head is represented by '#' and the tail by "*". What I am trying to do is implement this thinking:
Get input from user, move the head. Check in 4 possible directions of head's initial position, find the " * ", move it to the initial position of the head. Repeat till all '*' have been moved.
Obviously, I am doing something wrong while checking and/or moving, but I cannot quite figure out what parts I am treating wrong and after 2 days I am stuck with this as my closest to working version. I know I have been looking at it for too long, but I cant afford to take more time with it as it needs to be turned in.
Here is my code. I hope my error is very obvious to "fresh eyes".
main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
//get size of grid from args
int size;
size = atoi(argv[1]);
while(size < 6){
printf("Please insert a number greater than 6 so the game is
playable\n");
scanf("%d\n", &size);
}
char grid[size][size];
int sx,sy; //coordinates of snake head
int length = 2; //length of tail (without head)
int score = 0;
sx = 1;
sy = size/2;
//generate empty grid
int x, y;
for (y = 0; y < size; ++y) {
for (x = 0; x < size; ++x) {
if (y == 0 || y == size - 1) {
grid[y][x] = '-';
} else if (x == 0 || x == size - 1) {
grid[y][x] = '|';
} else {
grid[y][x] = ' ';
}
}
}
//initialize snake position
grid[sx][sy] = '#';
grid[sx+1][sy] = '*';
grid[sx+2][sy] = '*';
grid[sx+3][sy-6] = 'X';//just to test if it works, will be generated
randomly
//print grid
printGrid(size, grid);
play(sx,sy,size,grid,&length);
printf("*********GAME OVER********** \n final score: %d\n", score);
}
play() function. Practically all of the game.
int play(int x, int y, int size, char grid[size][size], int length){
int x0,y0,x1,y1,x2,y2,valid;//x0,y0 = current, x1,y1 next to check,
//x2,y2 = move there x0,y0
char end,dir; //what did the head land on
int score = 0;
int checkl = 0;
while(1){
scanf("%c", &dir);
switch(dir){
//move head, change head coordinates
case 'w': end = grid[x-1][y];
grid[x-1][y] = grid[x][y];
grid[x][y] = ' ';
x0 = x;
y0 = y;
x--;
break;
case 's': end = grid[x+1][y];
grid[x+1][y] = grid[x][y];
grid[x][y] = ' ';
x0 = x;
y0 = y;
x++;
break;
case 'a': end = grid[x][y-1];
grid[x][y-1] = grid[x][y];
grid[x][y] = ' ';
x0 = x;
y0 = y;
y--;
break;
case 'd': end = grid[x][y+1];
grid[x][y+1] = grid[x][y];
grid[x][y] = ' ';
x0 = x;
y0 = y;
y++;
break;
}
if ((end == '-') | (end =='|') | (end == '*')){
break;
}
printf("POINT 1\n");
do{
check(x0,y0,&x1,&y1, size, grid);
move(x0,y0, x2,y2, size, grid);
//if(valid == 1){
x2 = x0;
y2 = y0;
x0 = x1;
y0 = y1;
//}
checkl++;
printf("POINT 2\n");
}while(checkl <= length);
printf("POINT 3\n");
if (end == 'X'){
grid[x1][y1] = '*';
length++;
score += 10;
}
clearScreen();
printGrid(size, grid);
}
return score;
}
check() function. Implements the "look for the next '*'" process
int check(int x0, int y0, int *x1, int *y1, int size, char grid[size]
[size]){
if (grid[x0][y0-1] == '*'){
*x1 = x0;
*y1 = y0-1;
return 1;
}
else if (grid[x0][y0+1] == '*'){
*x1 = x0;
*y1 = y0+1;
return 1;
}
else if (grid[x0-1][y0] == '*'){
*x1 = x0-1;
*y1 = y0;
return 1;
}
else if (grid[x0+1][y0] == '*'){
*x1 = x0+1;
*y1 = y0;
return 1;
}
else
return 0;
}
move(). Self-explanatory
void move(int x0, int y0, int x2, int y2, int size, char grid[size]
[size]){
grid[x2][y2] = grid[x0][y0];
grid[x0][y0] = ' ';
}
A function to clear the window
void clearScreen()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);
}
PrintGrid. Also self explanatory, have it as a function for convenience
void printGrid(int size, char grid[size][size]){
int y,x;
for (y = 0; y < size; ++y) {
for (x = 0; x < size; ++x) {
printf("%c", grid[y][x]);
}
printf("\n");
}
}
I believe the error is either in check, move, and in the do-while condition with the length in play. However, as I said, with a lot of trial and error, I am still here.
All of this should be in one file, but i could not align it properly all together. Thanks for any help, very appreciated.
Edit: Edited part of the code out. Now what happens after running the code is: I get the grid and the snake. The head moves just fine, along with the first '" of the tail but the rest does not follow. When it "eats" an 'X', it grows the tail where it should, but the '' added disappears in the next move. Also, the score goes up 20 instead of 10 so for some reason it enters the if twice for one move. The movement of the head + the first '*' as well as the "game over" scenarios work as they should.
Tidbit: This is only 10 actual characters, not 12 because the \ characters don't count:
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
Better is to let the compiler count for you:
void clearScreen()
{
static const char CLEAR_SCREEN_ANSI[] = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, sizeof(CLEAR_SCREEN_ANSI)-1);
}
Note that CLEAR_SCREEN_ANSI is now an array, not a pointer, and the -1 is for the trailing NUL byte.
Aha:
if ((end == '-') | (end =='|') | (end == '*')){
The | are bitwise OR and you almost certainly mean logical OR ||:
if ((end == '-') || (end =='|') || (end == '*')){
Edit: They actually do the same thing in this case, but it's a common-enough error that it's worth fixing.
Hat tip to #Sami Kuhmonen
I've spent some time looking at this, and it's clear this is entirely about program logic and less about C, so that makes it a lot more difficult to dive into something unfamiliar (and without a real spec).
The general idea is that the program draws a square playing field with a three-part snake: an # for a head and two * for the body, and the keyboard lets you move the snake around on the field with awds keys. You can't move on top of yourself, you can't go past the walls, and you want to find the food at location marked with X.
I believe your approach was to look for * characters, but I am not sure how it could tell a * from the middle of the snake from one at the end, so my approach is to maintain an array of all the positions of the snake, and in the main function it's initialized:
int ysnake[4] = { 1, 2, 3, 4 };
int xsnake[4] = { 15, 15, 15, 16 };
plus an obvious function to draw the snake given the position array:
static void drawsnake(
int snakelen, int xsnake[snakelen], int ysnake[snakelen],
int size, char grid[size][size])
{
char snakechar = '#'; // start with snake head
for (int i = 0; i < snakelen; i++)
{
grid[ ysnake[i] ]
[ xsnake[i] ] = snakechar;
snakechar = '*'; // all the rest are tails
}
}
So now we have the play() function, and this required quite a bit of refactoring, but it works for me.
int play(
int snakelen, int xsnake[snakelen], int ysnake[snakelen],
int size, char grid[size][size])
{
int score = 0;
while (1)
{
char dir;
scanf ("%c", &dir);
int proposed_xhead = xsnake[0];
int proposed_yhead = ysnake[0];
switch (dir)
{
case 'w': // up
proposed_yhead--;
break;
case 's': // down
proposed_yhead++;
break;
case 'a': // left
proposed_xhead--;
break;
case 'd': // right
proposed_xhead++;
break;
default: // ignore this invalid char
continue;
}
// first make sure we didn't go off the edge of the field
if (proposed_yhead <= 0 || proposed_yhead >= (size-1)
|| proposed_xhead <= 0 || proposed_xhead >= (size-1))
{
// would head off the maze, LOSE
break;
}
const char char_at_proposed_head = grid[proposed_yhead][proposed_xhead];
// now make sure we're not bumping into ourself
if (char_at_proposed_head == '#' // we shouldn't ever be able to bump into our head
|| char_at_proposed_head == '*') // we bumped into our tail
{
break; // would run into ourself: LOSE
}
// NOW we know it's a legal move. Did we find the food?
if (char_at_proposed_head == 'X')
{
// found the food!
// update score, set new food, etc.
score += 10;
}
// blank out the former tail; we're about to lose that position
grid[ ysnake[ snakelen-1 ] ]
[ xsnake[ snakelen-1 ] ] = ' ';
// shift all items down in the array, dropping the last one
for (int i = snakelen-1; i > 0; i--)
{
xsnake[i] = xsnake[i-1];
ysnake[i] = ysnake[i-1];
}
xsnake[0] = proposed_xhead;
ysnake[0] = proposed_yhead;
drawsnake(snakelen, xsnake, ysnake, size, grid);
clearScreen ();
printGrid (size, grid);
}
return score;
}
There's a clear separation of duties, where the decoding of the direction key translates to a change in the snake's head position, then checking for out-of-bounds or running into yourself, and finding if you found the food.
Then we move the snake by shifting the entire array of positions: the tail element drops off and the new one shifts onto the front. Then we re-draw the snake with new positions.
The main function is the same as yours at the top to set up the grid, but the snake configuration and play looks like:
int main(int argc, argv)
{
...
int length = 4;
int ysnake[4] = { 1, 2, 3, 4 };
int xsnake[4] = { 15, 15, 15, 16 };
drawsnake(length, xsnake, ysnake, size, grid);
// Put the food somewhere
grid[sx + 3][sy - 6] = 'X'; //just to test if it works, will be generated randomly
printGrid (size, grid);
score = play(length, xsnake, ysnake, size, grid);
printf ("*********GAME OVER********** \n final score: %d\n", score);
}
I didn't really do anything meaningful with the score and have only barely tested it, but it does let the little guy slither around the screen, and it might be a bit easier to follow.
Good luck.

C: Comparing two elements not producing correct results?

My code is below, most of which may not be helpful, but maybe the problem lies outside of where I think it is. That being said, please read the following first because it gives the rundown of my code and states where I think the problem lies.
I'm trying to create a Battleship game in C. I first create two two-dimensional arrays, one representing the player's board and one representing the enemy's board. I fill both of them with periods. I print them out with numbers along the sides to make things look nice (using my initialPrintBoards function). I then set the locations of the enemy's ships by replacing some of the periods in the enemy's array with 's' and print it out to make sure they are where I want them to be. They are, which is great. I then have the player "fire" at the enemy's ship. This is done by replacing 's' in the enemy's array with 'x' (which represents a hit) or replacing '.' with 'o' (which represents a miss). I print this out, and everything works well.
Now, here's where I hit a problem. Up to this point, for the sake of testing, the enemy's ships have been completely visible to the player via my print method. I don't want that. So, what I figured I'd do is create a new print function (called printBoards) that does exactly what my previous print function does except it prints '.' on the board when it encounters 's' as an element in the enemy's array. My initial thought in accomplishing this was to use comparisons. Basically, if the element stored in the enemy's array at location whatever is 's', print '.', otherwise print out what's stored at that location in the array (which would be '.', 'x', or 'o'). Unfortunately, all it does is print all periods, even if there's an 'x' or 'o' stored at that location in the array. I'm at a loss as to why this is. I'm quite new to C (I've studied Java in the past), so maybe there's something about comparisons in C that I don't know about. But that's assuming the problem is with the comparisons, which it might not be.
Any help or hints would be greatly appreciated.
#include <stdio.h>
char playerBoard[8][8];
char enemyBoard[8][8];
void fillBoards()
{
int a;
for (a = 0; a < 8; a++)
{
int b;
for (b = 0; b < 8; b++)
{
enemyBoard[a][b] = '.';
}
}
int x;
for (x = 0; x < 8; x++)
{
int y;
for (y = 0; y < 8; y++)
{
playerBoard[x][y] = '.';
}
}
}
void initialPrintBoards()//This is used before the enemy's ships are set.
{
printf("Enemy Board\n*12345678\n");
int a;
for (a = 0; a < 8; a++)
{
printf("%d", a + 1);
int b;
for (b = 0; b < 8; b++)
{
printf("%c", enemyBoard[a][b]);
}
printf("\n");
}
printf("\n");
printf("Player Board\n*12345678\n");
int x;
for (x = 0; x < 8; x++)
{
printf("%d", x + 1);
int y;
for (y = 0; y < 8; y++)
{
printf("%c", playerBoard[x][y]);
}
printf("\n");
}
printf("\n");
}
void printGreeting()
{
printf("\nWelcome to Battleship!\n\n");
}
void setEnemyShips()
{
// Ship 1.
enemyBoard[3][2] = 's';
enemyBoard[4][2] = 's';
enemyBoard[5][2] = 's';
// Ship 2.
enemyBoard[1][1] = 's';
enemyBoard[1][2] = 's';
enemyBoard[1][3] = 's';
// Ship 3.
enemyBoard[6][5] = 's';
enemyBoard[6][6] = 's';
enemyBoard[6][7] = 's';
}
void playerFire()
{
if (enemyBoard[2][2] == 's')
{
enemyBoard[2][2] = 'x';
}
else
{
enemyBoard[2][2] = 'o';
}
}
void printBoards()//This is used after the enemy's ships are set.
{
printf("Enemy Board\n*12345678\n");
int a;
for (a = 0; a < 8; a++)
{
printf("%d", a + 1);
int b;
for (b = 0; b < 8; b++)
{
if (enemyBoard[1][0] == 's')
{
printf("%c", '.');
}
else
{
printf("%c", enemyBoard[1][0]);
}
}
printf("\n");
}
printf("\n");
printf("Player Board\n*12345678\n");
int x;
for (x = 0; x < 8; x++)
{
printf("%d", x + 1);
int y;
for (y = 0; y < 8; y++)
{
printf("%c", playerBoard[x][y]);
}
printf("\n");
}
printf("\n");
}
int main()
{
fillBoards();
printGreeting();
initialPrintBoards(); //This will print the boards before the enemy's ships are set.
setEnemyShips();
initialPrintBoards(); //This will end up printing the enemy ships' locations. Need a different print method.
playerFire();
initialPrintBoards(); //This prints to see if a hit or miss is properly printed.
printBoards(); //This prints to see if the ships are hidden and a hit or miss is properly printed.
return 0;
}
regarding this code, found in the printBoards() function:
if (enemyBoard[1][0] == 's')
{
printf("%c", '.');
}
else
{
printf("%c", enemyBoard[1][0]);
}
This always looks at the second row, first column to determine what is printed. As you saw, that is an error.
Suggest:
if (enemyBoard[a][b] == 's')
{
printf("%c", '.');
}
else
{
printf("%c", enemyBoard[a][b]);
}
Let the Board[][] variables contain the state '.' (water) or 'A', 'B' ... 'E' for the 5 ships.
When a ship is hit, change 'A' to 'a', etc.
When printing, pass in a control variable to control how the view is rendered: Enemy View, Player View, Programmer View.

Stop for loop upon returning value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the relevant function:
int computerChoice() {
int x, y;
for (y=0;y<=2;y++) {
for (x=0;x<=2;x++) {
if (matrix[y][x] == 'a') {
return(1);
} else {
matrix[y][x] = 'b';
return(0);
}
}
}
}
And this is the relevant main() code:
char matrix[3][3];
int duplicate1 = 0;
do {
duplicate1 = computerChoice();
} while(duplicate1 == 1);
I'm attempting to scan through a 3x3 two dimensional array and check sequentially if each one is equal to "a". If so I want the function to return a "1" and continue checking. Once it finds an entry that is not equal to "a" it should set that entry equal to b, return 0 and stop looping.
I get some really weird results. If [1][1] has an "a" in it, the function doesn't do anything but just "freezes". If [2][2] has an "a" in it, the function correctly places a "b" in the entry [1][1].
This also happens if other entries are already filled.
Any thoughts?
your implementation seems buggy the code should continue till it is finding 'a' in each cell.If it doesn't find 'a' it should set it to 'b' and return 0. the loop will stop automatically when you return.
If both the loop completes that means every cell in matrix contains 'a'. and you should return 1;
int computerChoice() {
int x, y;
for (y=0;y<=2;y++) {
for (x=0;x<=2;x++) {
if (matrix[y][x] == 'a') {
continue;
}
else {
matrix[y][x] = 'b';
return(0);
}
}
}
return 1;
}
I'm assuming what you mean is "I want to process the entire array, and return 1 if I ever found an a, and 0 otherwise". That can be achieved like so:
int found = 0;
for (size_t i = 0; i != 3; ++i)
{
for (size_t j = 0; j != 3; ++j)
{
if (matrix[i][j] == 'a') { found = 1; }
else { matrix[i][j] = 'b'; }
}
}
return found;
(By changing to ++found, you can also return the number of occurrences of 'a'.)
If on the other hand you want to return 0 as soon as you know there's a non-'a' in your data, it's simpler:
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 3; ++j)
if (matrix[i][j] != 'a') { matrix[i][j] = 'b'; return 0; }
return 1;
I don't exactly understand what you mean by "return 1 and continue checking". I will assume that you want to scan the whole array, and return 1 if every position is an a; otherwise, return 0 and set the first position that was not an a to b. Here's the code:
int computerChoice() {
int x, y;
for (y=0;y<=2;y++) {
for (x=0;x<=2;x++) {
if (matrix[y][x] != 'a') {
matrix[y][x] = 'b';
return 0;
}
}
}
return 1;
}

Saving Data in a Basic Tic-Tac-Toe Game

I'm relatively new to C. In Kochan's "Programming in C" I'm currently on if-else statements. I'm trying to program a basic tic-tac-toe game but I've run into some difficulty. I'm not sure how to save the board once a player has placed an x or an o. Here's the code I have so far:
#include <stdio.h>
int main (void)
{
int board = "_|_|_\n
_|_|_\n
| | \n";
int player1, player2;
printf ("Print %i", board)
printf("Player 1, it's your move:")
scanf("%i", player1)
if(player1 == "upLeft")
printf("x|_|_\n
_|_|_\n
_|_|_\n
etc.
Am I still too much of a beginner to implement this feature?
First of all, this doesn't make sense:
int board = "_|_|_\n
_|_|_\n
| | \n";
An int is not a string, and you can't assign a string to an int. In C, strings are arrays of characters:
char board[] = "_|_|_\n_|_|_\n | | \n";
That makes more sense. But it's really not a good way to store the state of a tic tac toe board. What you should do instead is store some value for each position on the board. Don't worry about the actual format of the board, you can format it as you like when you display it. So store your board this way:
char board[9] = "---------";
where a "-" means the space is empty. When the player moves, you replace the character at the appropriate position in the array with an "X" or an "O" (or 1 and 2, or any other values that work for you). When you get some input from the user, you'll change just the corresponding value in the board array. If you number the positions 0-8 starting from the top left corner, the rightmost position in the middle row would be position 5, for example. (Remember, C arrays are zero-based, so the first index is 0.) So if the user wants to put an X at that spot, you'd say:
board[5] = 'X';
Next, you might want to write a function that prints the board. That is where you'll insert whatever characters you like to draw the board.
Finally, you're going to want to use some sort of loop to repeatedly read the user's input, modify the state of the board, print the board, and maybe display a prompt.
First-things-first, you cannot store
"_|_|_\n
_|_|_\n
| | \n";
in an integer variable. You need variables of other types (like char *, char a[][], etc).
OTOH, the pseudocode is as follows. Please try and follow this to write a C program on your own.
Let row = 3 and column = 3
Declare an array[row][column] and fill it all with 0
Let 1 represent the input of user-1 and 2 represent the input of user-2 (in the array)
i.e. if a[2][2] = 1 means, user-1 marked that location.
while ( ! all_locations_filled() ) {
take input from user-1
if user-1 chooses a valid_location(location) to mark, then mark_location(user-1, location)
check if user-1 won_the_game(user-1), if so break and congratulate user-1!
take input from user-2
if user-2 chooses a valid_location(location) to mark, then mark_location(user-2, location)
check if user-2 won_the_game(user-2), if so break and congratulate user-2!
}
valid_location(location l)
{
return array[l.row][l.column] == 0;
}
mark_location(user u, location l)
{
array[l.row][l.column] = (u==user-1) ? 1 : 2;
}
display_board()
{
for i=0 to row
for j=0 to col
if array[i][j] == 0 print ""
else print array[i][j]
/* print blank when that location is not yet marked */
}
all_locations_filled()
{
for i=0 to row
for j=0 to col
if array[i][j] == 0
return false
return true
}
won_the_game(user u)
{
/* You need to write the logic here */
:P
}
You can use a 2d array to represent the board, and maybe some small int, with 0 as nothing there, 1 as X and 2 as 0.
You cant save the like that
int board = "X|_|_\n
_|_|_\n
| | \n";
you can have something like board[0][0]=1;
then you can iterate that array and if its 1 print the X.
It's a bit of a beginner question, but you're a beginner, so that's okay! Let's go with some leading questions:
So, you're saying you want to save the board state. What do you want to save? At any point in the program, what do you want to be able to look up? The history of the moves? What the board looks like? What each corner contains? Each of these suggests a different way to change a variable when you get some input from the user.
As other people have said, you can't do all those things with int type, and even if you could, this program is still way too hard and frustrating until you have a few more tools in your toolbox. Chapter 7 is Arrays, which will be very useful, and Chapter 10 is Character Strings, which will show you how to deal with all these strings the right way in C. So my suggestion to you is go through a few more chapters of the book and the big picture will start to make a bit more sense. Happy hacking!
Here is a small template like thing I have given write your own logic inside the GetBestMove() function for the computers move (if you are building AI) else replace the call for the GetBestMove by GetMove function in the StartGame function
#include<stdio.h>
#define HUMAN_WIN 1
#define DRAW 0
#define HUMAN_LOSE -1
#define HUMAN_COIN 'X'
#define COMP_COIN 'O'
#define DEFAULT 0xFFF
int main(void)
{
StartGame();
}
enum turnOf{ Human = 0, Computer};
enum gameEndState{Lose = -1, Draw, Win};
int humanMove = 1;
int computerMove = 1;
char _board[3][3] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
void StartGame()
{
enum turnOf turn = 0;
enum gameEndState state = -2;
while(1)
{
turn = 1 - turn;
if(turn == Human)
{
GetMove();
UpdateBoard(humanMove, turn);
}
else if(turn == Computer)
{
GetBestMove(turn);
UpdateBoard(computerMove, turn, _board);
}
state = win(_board);
switch(state)
{
case Win:
NotifyUser("You win.");exit(0);
case Draw:
NotifyUser("Match draw.");exit(0);
case Lose:
NotifyUser("You Lose.");exit(0);
}
}
}
int depth = 0;
int GetBestMove(enum turnOf turn, int *x, int *y)
{
depth++;
int i, j, MOV = -10, BESTMOVx, BESTMOVy;
enum turnOf now = turn;
char pebble = (now == Human) ? HUMAN_COIN : COMP_COIN;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(_board[i][j] == '-')
{
_board[i][j] = pebble;
now = 1 - now;
int condition = win(_board);
if(condition != DRAW || condition != DEFAULT)
{
return (condition == HUMAN_LOSE) ? (depth - 10) : (10 - depth);
}
else
{
int state = GetBestMove(now, BESTMOVx, BESTMOVy);
if(state > MOV)
{
MOV = state;
}
}
}
}
}
}
int win(char a[3][3])
{
char pebble = HUMAN_COIN;
int i, j, p = 0;
i=0;
for(j = 0; j < 3; j++)
if(a[i][j] == pebble && a[i+1][j] == pebble && a[i+2][j] == pebble) return HUMAN_WIN;
j=0;
for(i = 0; i < 3; i++)
if(a[i][j] == pebble && a[i][j+1] == pebble && a[i][j+2] == pebble) return HUMAN_WIN;
if(a[0][0] == pebble && a[1][1] == pebble && a[2][2] == pebble) return HUMAN_WIN;
else if(a[0][2] == pebble && a[1][1] == pebble && a[2][0] == pebble) return HUMAN_WIN;
/// Recheck for lose
pebble = COMP_COIN;
i=0;
for(j = 0; j < 3; j++)
if(a[i][j] == pebble && a[i+1][j] == pebble && a[i+2][j] == pebble) return HUMAN_LOSE;
j=0;
for(i = 0; i < 3; i++)
if(a[i][j] == pebble && a[i][j+1] == pebble && a[i][j+2] == pebble) return HUMAN_LOSE;
if(a[0][0] == pebble && a[1][1] == pebble && a[2][2] == pebble) return HUMAN_LOSE;
else if(a[0][2] == pebble && a[1][1] == pebble && a[2][0] == pebble) return HUMAN_LOSE;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
if(a[i][j] == '-') p++;
if(p == 0) return DRAW;
return DEFAULT;
}
void GetMove()
{
int x, y;
LoadFrame(_board);
printf("\nEnter your move : ");
humanMove = getche() - 48;
if(!(humanMove > 0 && humanMove < 10))
{
NotifyUser("Enter a valid location.");
GetMove();
}
GetCordinates(&x, &y, humanMove);
if(_board[x][y] != '-')
{
NotifyUser("The place is nonEmpty.");
GetMove();
}
}
void UpdateBoard(int move, enum turnOf player, char board[3][3])
{
int x, y;
char pebble = (player == Human) ? HUMAN_COIN : COMP_COIN;
GetCordinates(&x, &y, move);
board[x][y] = pebble;
LoadFrame(board);
}
void LoadFrame(char board[3][3])
{
int x, y;
system("cls");
for(x = 0; x < 3; x++)
{
printf("\n\t ");
for(y = 0; y < 3; y++)
{
printf(" %c ", board[x][y]);
}
}
}
void GetCordinates(int *x, int *y, int move)
{
switch(move)
{
case 1: *x = 0; *y = 0; break;
case 2: *x = 0; *y = 1; break;
case 3: *x = 0; *y = 2; break;
case 4: *x = 1; *y = 0; break;
case 5: *x = 1; *y = 1; break;
case 6: *x = 1; *y = 2; break;
case 7: *x = 2; *y = 0; break;
case 8: *x = 2; *y = 1; break;
case 9: *x = 2; *y = 2; break;
}
}
void NotifyUser(const char* message)
{
printf("\n\n%s\a", message);
getch();
system("cls");
LoadFrame(_board);
}

Resources