how to switch between players with enum from .h? - c

Does anyone know why when I run this code it goes for P2_TURN and how to make this enum active that when P2 move then P1 goes and whoever win the correct statement will be given from print_status()… also is there an easier/cleaner way to assign the winner. At the void process_move(struct game* p_game_info) is there a way to replace digits 0-8 with user input without switch case….Sorry for being a bit chaotic with this query but it’s almost 4 in the morning so hope you’ll forgive me….
game.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
void play_game()
{
struct game *p_game_info = 0;
p_game_info = malloc(sizeof(struct game));
initialise_game (p_game_info, "John", "Annie");
draw_banner();
display_board(p_game_info->board);
system("cls");
display_board_positions ();
draw_banner();
process_move( p_game_info);
display_board(p_game_info->board);
print_status ( p_game_info );
}
void initialise_game(struct game* p_game_info, char* name1, char* name2)
{
for (int r=0; r<3; r++)
for(int c=0; c<3; c++)
p_game_info->board[r][c] = SPACE;
display_board_positions ();
p_game_info->status=P1_TURN;
// p_game_info->finished = False;
strncpy(p_game_info->playerNames[0], name1,MAX_NAME_LEN);
strncpy(p_game_info->playerNames[1], name2,MAX_NAME_LEN);
}
void draw_banner(){
printf("---------------\n");
printf(" GAME STATUS \n");
printf("---------------\n");
}
void display_board( char board[3][3]){
printf("---------------\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf(" %c ", board[i][j]);
if (j != 2)
printf("|");
}
if (i != 2)
printf("\n-----------");
printf("\n");
}
printf("---------------\n");
}
void print_status (struct game*p_game_info ){
if ((p_game_info->finished=False)&&
(p_game_info->status=P1_TURN))
{
printf("John's Turn\n");
}
else if ((p_game_info->finished=False)&&
(p_game_info->status=P2_TURN))
{
printf("Annie's Turn\n");
}
else if
(p_game_info->status==P1_WON)
{
printf( "Well done John, you have won\n");
}
else if
(p_game_info->status==P2_WON)
{
printf("Well done Annie, you have won\n");
}
else if
(p_game_info->status==DRAW)
{
printf("Game Over It is a draw\n");
}
}
void display_board_positions (){
int count = 0;
printf("---------------\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf(" %d ", count);
count++;
if (j != 2)
printf("|");
}
if (i != 2)
printf("\n-----------");
printf("\n");
}
printf("---------------\n");
}
void process_move(struct game* p_game_info){
if (p_game_info->status==P1_TURN){
printf("enter number for 'X' from 0-8\n");
char ckey = (char) getchar();
switch (ckey){
case '0':
if(p_game_info->board[0][0]==SPACE){
p_game_info->board[0][0]=X_SYMBOL;
}
break;
case '1':
if(p_game_info->board[0][1]==SPACE){
p_game_info->board[0][1]=X_SYMBOL;
}
break;
case '2':
if(p_game_info->board[0][2]==SPACE){
p_game_info->board[0][2]=X_SYMBOL;
}
break;
case '3':
if(p_game_info->board[1][0]==SPACE){
p_game_info->board[1][0]=X_SYMBOL;
}
break;
case '4':
if(p_game_info->board[1][1]==SPACE){
p_game_info->board[1][1]=X_SYMBOL;
}
break;
case '5':
if(p_game_info->board[1][2]==SPACE){
p_game_info->board[1][2]=X_SYMBOL;
}
break;
case '6':
if(p_game_info->board[2][0]==SPACE){
p_game_info->board[2][0]=X_SYMBOL;
}
break;
case '7':
if(p_game_info->board[2][1]==SPACE){
p_game_info->board[2][1]=X_SYMBOL;
}
break;
case '8':
if(p_game_info->board[2][1]==SPACE){
p_game_info->board[2][1]=X_SYMBOL;
}
break;
default:
;
}p_game_info->status=P2_TURN
}
else if (p_game_info->status=P2_TURN){
printf("enter number for 'O' from 0-8\n");
char ckey = (char) getchar();
switch (ckey){
case '0':
if(p_game_info->board[0][0]==SPACE){
p_game_info->board[0][0]=O_SYMBOL;
}
break;
case '1':
if(p_game_info->board[0][1]==SPACE){
p_game_info->board[0][1]=O_SYMBOL;
}
break;
case '2':
if(p_game_info->board[0][2]==SPACE){
p_game_info->board[0][2]=O_SYMBOL;
}
break;
case '3':
if(p_game_info->board[1][0]==SPACE){
p_game_info->board[1][0]=O_SYMBOL;
}
break;
case '4':
if(p_game_info->board[1][1]==SPACE){
p_game_info->board[1][1]=O_SYMBOL;
}
break;
case '5':
if(p_game_info->board[1][2]==SPACE){
p_game_info->board[1][2]=O_SYMBOL;
}
break;
case '6':
if(p_game_info->board[2][0]==SPACE){
p_game_info->board[2][0]=O_SYMBOL;
}
break;
case '7':
if(p_game_info->board[2][1]==SPACE){
p_game_info->board[2][1]=O_SYMBOL;
}
break;
case '8':
if(p_game_info->board[2][1]==SPACE){
p_game_info->board[2][1]=O_SYMBOL;
}
break;
default:
;
}}
}
void finished_game(struct game* p_game_info){
if((p_game_info->board[0][0]==O_SYMBOL)&&(p_game_info->board[0] [1]==O_SYMBOL)&&(p_game_info->board[0][2]==O_SYMBOL)||
(p_game_info->board[1][0]==O_SYMBOL)&&(p_game_info->board[1][1]==O_SYMBOL)&& (p_game_info->board[1][2]==O_SYMBOL)||
(p_game_info->board[2][0]==O_SYMBOL)&&(p_game_info->board[2][1]==O_SYMBOL)&&(p_game_info->board[2][2]==O_SYMBOL)||
(p_game_info->board[0][0]==O_SYMBOL)&&(p_game_info->board[1][0]==O_SYMBOL)&&(p_game_info->board[2][0]==O_SYMBOL)||
(p_game_info->board[0][1]==O_SYMBOL)&&(p_game_info->board[1][1]==O_SYMBOL)&&(p_game_info->board[2][1]==O_SYMBOL)||
(p_game_info->board[0][2]==O_SYMBOL)&&(p_game_info->board[1][2]==O_SYMBOL)&&(p_game_info->board[2][2]==O_SYMBOL)||
(p_game_info->board[0][0]==O_SYMBOL)&&(p_game_info->board[1][1]==O_SYMBOL)&&(p_game_info->board[2][2]==O_SYMBOL)||
(p_game_info->board[0][2]==O_SYMBOL)&&(p_game_info->board[1][1]==O_SYMBOL)&&(p_game_info->board[2][0]==O_SYMBOL))
{
p_game_info->status=P2_WON;
return;
}
else if((p_game_info->board[0][0]==X_SYMBOL)&&(p_game_info->board[0][1]==X_SYMBOL)&&(p_game_info->board[0][2]==X_SYMBOL)||
(p_game_info->board[1][0]==X_SYMBOL)&&(p_game_info->board[1][1]==X_SYMBOL)&&(p_game_info->board[1][2]==X_SYMBOL)||
(p_game_info->board[2][0]==X_SYMBOL)&&(p_game_info->board[2][1]==X_SYMBOL)&&(p_game_info->board[2][2]==X_SYMBOL)||
(p_game_info->board[0][0]==X_SYMBOL)&&(p_game_info->board[1][0]==X_SYMBOL)&&(p_game_info->board[2][0]==X_SYMBOL)||
(p_game_info->board[0][1]==X_SYMBOL)&&(p_game_info->board[1][1]==X_SYMBOL)&&(p_game_info->board[2][1]==X_SYMBOL)||
(p_game_info->board[0][2]==X_SYMBOL)&&(p_game_info->board[1][2]==X_SYMBOL)&&(p_game_info->board[2][2]==X_SYMBOL)||
(p_game_info->board[0][0]==X_SYMBOL)&&(p_game_info->board[1][1]==X_SYMBOL)&&(p_game_info->board[2][2]==X_SYMBOL)||
(p_game_info->board[0][2]==X_SYMBOL)&&(p_game_info->board[1][1]==X_SYMBOL)&&(p_game_info->board[2][0]==X_SYMBOL))
{
p_game_info->status=P1_WON;
return;
}
else
{
p_game_info->status=DRAW;
return;
}
}
game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#define MAX_NAME_LEN 50
enum Bool { False, True };
enum status { P1_TURN, P2_TURN, P1_WON, P2_WON, DRAW };
typedef enum Bool boolean;
static const char SPACE= '-';
static const char X_SYMBOL = 'X';
static const char O_SYMBOL = 'O';
struct game {
char board[3][3];
char playerNames[2][MAX_NAME_LEN];
int status;
boolean finished;
};
void play_game();
void initialise_game(struct game* p_game_info, char* name1, char* name2);
void draw_banner();
void display_board( char board[3][3]);
void print_status (struct game*p_game_info );
void display_board_positions ();
void process_move(struct game* p_game_info);
void finished_game(struct game* p_game_info);
#endif // game
Current output
---------------
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
GAME STATUS
enter number for 'O' from 0-8
2
- | - | O
- | - | -
- | - | -
Process returned 0 (0x0) execution time : 7.731 s
Press any key to continue.

Your question is indeed chaotic. I'm not sure what you really want to know, but notice that 'else if (p_game_info->status=P2_TURN){' is wrong. You need a double equal sign there.

Related

Snake game C code is not working for bigger length and height values

I am writing code for snake game using C programming language. In my code, when I am trying to take bigger length and height value for game border then output is somehow got distorted. Please let me know how can I make border which could cover whole console window and what is the issue with existing code.
My code:-
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define MAXTL 100
int len=20,wid=20;
int headX,headY,fruitX,fruitY,gameOver,level,flag=0,score=0;
int tailX[MAXTL];int tailY[MAXTL];
int countTail=0;
void draw();
void defaultsetting();
void userinput();
void my_sleep(int level);
//void delay();
void draw(){
system("cls");
for(int i=0;i<len;i++)
{
for(int j=0;j<wid;j++){
if(i==0 || i==len-1){
printf("=");
}
else if(j==0 || j==wid-1){
printf("#");
}
else{
if(i==headX && j==headY){
printf("O");
}
else if(i==fruitX && j==fruitY){
printf("*");
}
else{
int tail=0;
for(int k=0;k<countTail;k++){
if(i==tailX[k]&& j==tailY[k]){
printf("-");
tail=1;
}
}
if(tail==0){
printf(" ");
}
}
}
}
printf("\n");
}
}
void defaultsetting(){
gameOver=0;
srand(time(0));
headX=len/2;
headY=wid/2;
do{
fruitX=rand()%20;
fruitY=rand()%20;
}while(fruitX==0||fruitY==0);
}
void userinput(){
if(kbhit()){
switch(getch()){
case 72://up
flag=1;
break;
case 75://left
flag=2;
break;
case 77://right
flag=3;
break;
case 80://down
flag=4;
break;
case 'q':
gameOver=1;
break;
}
}
}
void logic(){
srand(time(0));
int i,prev2X,prev2Y;
int prevX=tailX[0];
int prevY=tailY[0];
tailX[0]=headX;
tailY[0]=headY;
for(i=1;i<countTail;i++){
prev2X=tailX[i];
prev2Y=tailY[i];
tailX[i]=prevX;
tailY[i]=prevY;
prevX=prev2X;
prevY=prev2Y;
}
switch(flag){
case 1:
headX--;
break;
case 2:
headY--;
break;
case 3:
headY++;
break;
case 4:
headX++;
break;
default:
//printf("Inside default");
break;
}
if(headX <0|| headX>len||headY<0||headY>wid){
gameOver=1;
}
for(i=0;i<countTail;i++){
if(i==tailX[i]&& i==tailY[i]){
gameOver=1;
}
}
if(headX==fruitX&& headY==fruitY)
{
do{
fruitX=rand()%20;
fruitY=rand()%20;
}while(fruitX==0 ||fruitY==0);
score+=10;
countTail++;
}
}
int main(){
int m,n;
char c;
defaultsetting();
printf("Enter level of game\n");
scanf("%d",&level);
while(!gameOver)
{
draw();
userinput();
logic();
my_sleep(level);
}
printf("Your Score =%d",score);
return 0;
}
void my_sleep(int level){
if(level==1){
sleep(1);
}
else if(level==2){
sleep(0.4);
}
else if(level==3){
sleep(0.02);
}
}

C program query

So this is my program and the number, name, address doesn't print again after single execution and also If the seat is already taken there should be "seat is taken already, please try again" which I'm confused about
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct passenger
{
char name[20];
char address[30];
int age;
};
struct passenger data;
struct rwcl
{
int row;
char col;
};
int clmn, i, j;
int arr[5][5];
struct rwcl number;
for(i=0;i<5;i++)
{
printf("Enter Your Name: ");
scanf("\n");
gets(data.name);
printf("Enter Your Address: ");
scanf("\n");
gets(data.address);
printf("Enter Your Age: ");
scanf("%d", &data.age);
printf("\nAll aboard! You may now choose your desired seat/s.");
while(i<5){
for(j=0;j<5;j++){
if (j == 0) {
arr[i][j] = i+1;
}
if(j == 1){
arr[i][j] = 'A';
}
if(j== 2){
arr[i][j] = 'B';
}
if(j == 3){
arr[i][j] = 'C';
}
if(j== 4){
arr[i][j] = 'D';
}
}
i++;
}
printrwcl:
printf("\n\n");
for(i=0;i<5;i++){
for(j=0;j<5;++j){
if(j == 0 ){
printf("%-5d", arr[i][j]);
}
else {
printf("%-5c", arr[i][j]);
}
}
if(j==5) {
printf("\n");
}
}
printf("\n");
rowselect:
printf("Choose a row between 1,2,3,4,5 or 6 for cancellation: ");
scanf("%d", &number.row);
if(number.row < 0 || number.row > 6) {
printf("\nPlease, re-enter. Thank you.\n");
goto rowselect;
}
if(number.row == 6) {
printf("Recorded, thank you.");
exit(0);
}
columnselect:
printf("Choose a letter between A,B,C,D: ");
scanf("\n");
scanf("%c", &number.col);
switch(number.col)
{
case 'A':
clmn = 1;
break;
case 'B':
clmn = 2;
break;
case 'C':
clmn = 3;
break;
case 'D':
clmn = 4;
break;
}
if(arr[number.row-1][clmn] == 'X')
{
printf("Seat is taken. Please choose a different one.");
}
else
{
printf("Seat %d%c has been reserved.", number.row, number.col);
arr[number.row-1][clmn] = 'X';
}
goto printrwcl;
}
}

How to properly make a dynamic command-line menu?

More precisely.. How do I add a "go back to main menu" function as all other softwares and games have?
void showMenu()
{
puts( "1. Create school\n"
"2. Add room\n"
"3.Add student to room\n"
"4.Find student\n"
"5. Show students in room\n"
"\n" "6. Exit");
}
int main()
{
clrscr();
studentList *foundStudent;
int input;
showMenu();
while( scanf("%d", &input) )
{
if(input == 6)
{
periods("Exiting");
break;
}
if(input == 1)
{
school *school;
school = createSchool();
}
if(input == 2)
{
int room, roomNr;
printf("Enter room Nr. and Class:");
scanf("%d %d", &room, &roomNr);
}
}
return 0;
}
Anything I attempted didn't work and just created more redundancy, never expected how goto can be so confusing.
Although switch makes more sense, I don't believe it fixes my problem.
Here is a working example.
The solution for this problem was simply making a "menu within a menu" kind of, I did use some gotos but as far as commandline menu goes, this may be all.
#define clrscr() printf("\e[1;1H\e[2J")
void periods(char* message)
{
const int trigger = 500; // ms
const int numDots = 3;
while (1)
{
// Return and clear with spaces, then return and print prompt.
printf("\r%*s\r%s", sizeof(message) - 1 + numDots, "", message);
fflush(stdout);
// Print numDots number of dots, one every trigger milliseconds.
for (int i = 0; i < numDots; i++)
{
usleep(trigger * 1000);
fputc('.', stdout);
fflush(stdout);
}
break;
}
}
void showmenu()
{
clrscr();
puts("1. New Game\n"
"2. Load Game\n"
"3. Credits\n\n"
"4. Exit\n" );
}
int checkString(char *str)
{
int status = 0;
int ln = strlen(str);
for(int i = 0; i < ln; i++)
{
if(isdigit( str[i] ) )
status = 1;
break;
}
return status;
}
int main(){
char choice;
clrscr();
int status, isNum = -101;
char *name, *buffer, YN;
showmenu();
while(1)
{
scanf(" %c", &choice);
if( isdigit(choice) )
{
break;
}
else
{
fflush(stdin);
printf("Please only enter numbers!\n");
sleep(1);
showmenu();
}
}
do{
switch(choice)
{
case '1':
{
createG:;
clrscr();
printf("Enter name or press 0 to return\n");
scanf("%s", buffer);
status = checkString(buffer);
if(status == 1)
{
clrscr();
break;
}
clrscr();
name = (char*)malloc(strlen(buffer)+1);
strcpy(name, buffer);
printf("New game created, welocome %s!\n");
for(int i = 0; i < 5; i++)
{
sleep(1);
printf("%d\n", i);
}
break;
}
case '2':
{
int what;
caseL:;
clrscr();
printf("No saves!\n Create new game? [Y/N] \n to return press 0\n");
scanf(" %c", &YN);
if(YN == 'N') what = 0;
if(YN == '0') what = -1;
if(YN == 'Y') what = 1;
switch(YN)
{
case 'N':
{
goto caseL;
}
case '0':
{
break;
}
case 'Y':
{
choice = 1;
goto createG;
break;
}
}
break;
}
case '3':
{
periods("Hello World");
break;
}
case '4':
{
clrscr();
periods("Goodbye");
clrscr();
exit(1);
}
default:
{
printf("Wrong input\n Try again");
sleep(1);
break;
}
}
}while(choice != -2);
return 0;
}
This may still need a lot of error checking and handling for "unexpected" inputs but it answers the problem.

Tic Tac Toe - Turns C

I've been turning around and around trying to figure out how to give my players turns. The questions is for there to be two players playing tic tac toe but I can't figure how to do that. Here's my code:
#include <stdio.h>
#include <stdlib.h>
void displayBoard(char [3][3]);enter code here
int playerType (int player, char boardArray[3][3]);
int selectLocation(char [3][3], int , int );
char setTurn(char [3][3], int , int , char );
int main()
{
int player,location;
char position;
char boardArray[3][3]={{'1','2','3'},
{'4','5','6'},
{'7','8','9'}};
player= playerType (player, boardArray);
int i;
for(i=0;i<9;i++)
{
if (player==3)
break;
else{
location=selectLocation(boardArray, player, location);
position=setTurn(boardArray, location, player, position);
}
}
return 0;
}
void displayBoard(char boardArray [3][3]) //This displays the tic tac toe board
{
printf("\t%c|%c|%c\n", boardArray[0][0], boardArray[0][1], boardArray[0][2]);
printf("\t%c|%c|%c\n", boardArray[1][0], boardArray[1][1], boardArray[1][2]);
printf("\t%c|%c|%c\n", boardArray[2][0], boardArray[2][1], boardArray[2][2]);
}
int playerType (int player, char boardArray [3][3]) //This decides who plays first
{
player=0;
printf("Enter 1 for Player X.\n");
printf("Enter 2 for Player O.\n");
printf("Enter 3 to Quit. \n");
scanf("%d", &player);
if (player == 1)
{
printf("You're player X.\n");
displayBoard(boardArray);
}
else if (player == 2)
{
printf("You're player O.\n");
displayBoard(boardArray);
}
else if(player == 3)
printf("You Quit.\n");
else
printf("Invalid Entry.\n");
return player;
}
int selectLocation(char boardArray[3][3], int player, int location) //This takes in the location
{
printf("Pick a location from 1-9.\n");
scanf("%d", &location);
return location;
}
char setTurn(char boardArray[3][3], int location, int player, char position) //This outputs the location
{
if (player == 1)
{
switch(location)
{
case 1:
{
boardArray[0][0]='x';
break;
}
case 2:
{
boardArray[0][1]='x';
break;
}
case 3:
{
boardArray[0][2]='x';
break;
}
case 4:
{
boardArray[1][0]='x';
break;
}
case 5:
{
boardArray[1][1]='x';
break;
}
case 6:
{
boardArray[1][2]='x';
break;
}
case 7:
{
boardArray[2][0]='x';
break;
}
case 8:
{
boardArray[2][1]='x';
break;
}
case 9:
{
boardArray[2][2]='x';
break;
}
default:
printf("invalid");
}
}
else if (player == 2)
{
switch(location)
{
case 1:
{
boardArray[0][0]='O';
break;
}
case 2:
{
boardArray[0][1]='O';
break;
}
case 3:
{
boardArray[0][2]='O';
break;
}
case 4:
{
boardArray[1][0]='O';
break;
}
case 5:
{
boardArray[1][1]='O';
break;
}
case 6:
{
boardArray[1][2]='O';
break;
}
case 7:
{
boardArray[2][0]='O';
break;
}
case 8:
{
boardArray[2][1]='O';
break;
}
case 9:
{
boardArray[2][2]='O';
break;
}
default:
printf("Invalid");
}
}
printf("\t%c|%c|%c\n", boardArray[0][0], boardArray[0][1], boardArray[0][2]);
printf("\t%c|%c|%c\n", boardArray[1][0], boardArray[1][1], boardArray[1][2]);
printf("\t%c|%c|%c\n", boardArray[2][0], boardArray[2][1], boardArray[2][2]);
return position;
}
Change player selection part player= playerType (player, boardArray); inside for loop like this -
int player,location;
char position;
char boardArray[3][3]={{'1','2','3'},
{'4','5','6'},
{'7','8','9'}};
int i;
for(i=0;i<9;i++)
{
player= playerType (player, boardArray);
if (player==3)
break;
else
{
location=selectLocation(boardArray, player, location);
position=setTurn(boardArray, location, player, position);
}
}

Pointer assignment using malloc going wrong [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Please feel free to copy paste and try it out, it stops working halfway through function handget when individual hand is assigned a space in memory
/*
* A program that is meant to classify five-card poker hands.
*Is still in progress
*/
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define SEED 7
#define SIZE 5
/*
*
*/
typedef enum suits {
clubs,
diamonds,
hearts,
spades
} suits;
typedef enum values {
zero,
one, // not used, but ensures numerical values correspond
two,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
jack,
queen,
king,
ace
} values;
typedef struct cards {
suits suit;
values value;
} cards;
int islegal(cards *hand, int nr_of_cards);
/* Given any number of cards, determines whether any duplicates
* or false cards are present or not. If so, returns 0, otherwise 1.
*/
int flop(cards **handPtr);
/* Asks the user to input a hand of cards;
* returns the number of cards being input
*/
int *rawtoflop (cards *handPtr, int nr_of_cards);
int hander(cards **handPtr,int counter);
int handget(cards **playerhands,cards *thishands, int handsize);
void printsuit(suits thissuit);
/* Prints the enum-type suits
*/
void printvalue(values thisvalue);
/* Prints the enum-type values
*/
void printhand(cards *hand, int nr_of_cards);
/* Prints a hand of cards without further processing
*/
int main(void) {
cards *thishand, *playerhands;
flop(&thishand);
printhand(thishand,6);
handget(&playerhands,thishand,6);
return 0;
}
int islegal(cards *hand, int nr_of_cards) {
int i, fulldeck[4][13]={0};
int current_value, current_suit;
cards *current_card = hand;
int legal = 1;
for (i=0; i<nr_of_cards; i++) {
current_value = (int) (*current_card).value;
current_suit = (*current_card).suit;
// if the current card has value zero, it is not a valid hand
if (current_value==0) {
legal = 0;
break;
}
//check if the current card already appears, if yes invalid hand, if no,
//change the flag for that card in the full deck.
//Since (two) will correspond to fulldeck[.][0] there is a -2 offset.
if ( (fulldeck[current_suit][current_value - 2]) > 0 ) {
legal = 0;
break;
} else {
fulldeck[current_suit][current_value - 2]++;
}
current_card++;
}
return legal;
}
int flop(cards **handPtr) {
int i,*q=NULL,n=NULL;
int j[5]={0,0,0,0,0};
int k[5]={1,1,1,1,1},nr_of_cards = 5;//for more/less cards CHANGE HERE!
char rawsuit, rawcard[4];
// allocate the required amount of memory for your cards
(*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));
n=memcmp(j,k,sizeof(j));
while(n!=0) {
printf("Please input the five cards on the table: ");
q=rawtoflop(*handPtr,nr_of_cards);
for (i=0;i<nr_of_cards;i++) {
j[i]=*(q+i);
}
n=memcmp(j,k,sizeof(j));
}
free(&handPtr);
return nr_of_cards;
}
int *rawtoflop (cards *handPtr, int nr_of_cards){
int i,m[5],*mPtr;
char rawsuit, rawcard[4];
mPtr=m;
// ask for the cards
for (i=0; i<nr_of_cards; i++)/* do */{
scanf("%3s", &rawcard);
rawsuit = rawcard[0];
if (rawcard[1]=='1') {
if (rawcard[2]=='0') {
(handPtr)[i].value = ten;
} else {
(handPtr)[i].value = zero;
}
} else if (rawcard[1]=='2') {
(handPtr)[i].value = two;
} else if (rawcard[1]=='3') {
(handPtr)[i].value = three;
} else if (rawcard[1]=='4') {
(handPtr)[i].value = four;
} else if (rawcard[1]=='5') {
(handPtr)[i].value = five;
} else if (rawcard[1]=='6') {
(handPtr)[i].value = six;
} else if (rawcard[1]=='7') {
(handPtr)[i].value = seven;
} else if (rawcard[1]=='8') {
(handPtr)[i].value = eight;
} else if (rawcard[1]=='9') {
(handPtr)[i].value = nine;
} else if (rawcard[1]=='J') {
(handPtr)[i].value = jack;
} else if (rawcard[1]=='Q') {
(handPtr)[i].value = queen;
} else if (rawcard[1]=='K') {
(handPtr)[i].value = king;
} else if (rawcard[1]=='A') {
(handPtr)[i].value = ace;
} else {
(handPtr)[i].value = zero;
}
switch (rawsuit) {
case 'h':
(handPtr)[i].suit = hearts;
break;
case 'd':
(handPtr)[i].suit = diamonds;
break;
case 'c':
(handPtr)[i].suit = clubs;
break;
case 's':
(handPtr)[i].suit = spades;
break;
default:
(handPtr)[i].value = zero;
}
m[i]=(islegal(handPtr,i+1));
}
return mPtr;
}
int hander(cards **handPtr,int counter) {
int i, nr_of_cards = 2;
char rawsuit, rawcard[4];
if(counter==0){
// allocate the required amount of memory for your cards
(*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));
}
// ask for the cards
for (i=0; i<nr_of_cards; i++) do {
scanf("%3s", &rawcard);
rawsuit = rawcard[0];
if (rawcard[1]=='1') {
if (rawcard[2]=='0') {
(*handPtr)[i].value = ten;
} else {
(*handPtr)[i].value = zero;
}
} else if (rawcard[1]=='2') {
(*handPtr)[i].value = two;
} else if (rawcard[1]=='3') {
(*handPtr)[i].value = three;
} else if (rawcard[1]=='4') {
(*handPtr)[i].value = four;
} else if (rawcard[1]=='5') {
(*handPtr)[i].value = five;
} else if (rawcard[1]=='6') {
(*handPtr)[i].value = six;
} else if (rawcard[1]=='7') {
(*handPtr)[i].value = seven;
} else if (rawcard[1]=='8') {
(*handPtr)[i].value = eight;
} else if (rawcard[1]=='9') {
(*handPtr)[i].value = nine;
} else if (rawcard[1]=='J') {
(*handPtr)[i].value = jack;
} else if (rawcard[1]=='Q') {
(*handPtr)[i].value = queen;
} else if (rawcard[1]=='K') {
(*handPtr)[i].value = king;
} else if (rawcard[1]=='A') {
(*handPtr)[i].value = ace;
} else {
(*handPtr)[i].value = zero;
}
switch (rawsuit) {
case 'h':
(*handPtr)[i].suit = hearts;
break;
case 'd':
(*handPtr)[i].suit = diamonds;
break;
case 'c':
(*handPtr)[i].suit = clubs;
break;
case 's':
(*handPtr)[i].suit = spades;
break;
default:
(*handPtr)[i].value = zero;
}
} while (!islegal(*handPtr, i+1));
return nr_of_cards;
}
int handget(cards **playerhand,cards *thishands, int handsize) {
cards *player=NULL,*individualhand=NULL;
int nr_players=1;
(*playerhand) = (cards *) malloc(7*sizeof(cards));
memcpy(*playerhand,thishands,handsize*sizeof(cards));
printf("Please enter the cards for player 1: ");
hander(&player,0);
memcpy(*playerhand+5,player,7*sizeof(cards));
printf("1 we made it this far chaps!!!\n");
individualhand =(cards *) malloc(7*sizeof(cards));//THIS IS WHERE IT ALL GOES WRONG!!
printf("2 we made it this far chaps!!\n");
return nr_players;
}
void printsuit(suits thissuit) {
switch (thissuit) {
case diamonds:
printf("d");
break;
case clubs:
printf("c");
break;
case hearts:
printf("h");
break;
case spades:
printf("s");
break;
}
return;
}
void printvalue(values thisvalue) {
switch (thisvalue) {
case two:
printf("2");
break;
case three:
printf("3");
break;
case four:
printf("4");
break;
case five:
printf("5");
break;
case six:
printf("6");
break;
case seven:
printf("7");
break;
case eight:
printf("8");
break;
case nine:
printf("9");
break;
case ten:
printf("10");
break;
case jack:
printf("J");
break;
case queen:
printf("Q");
break;
case king:
printf("K");
break;
case ace:
printf("A");
break;
}
return;
}
void printhand(cards *hand, int nr_of_cards) {
int i;
for (i=0; i<nr_of_cards; i++) {
printsuit((hand[i]).suit);
printvalue((hand[i]).value);
printf(" ");
}
printf("\n");
return;
}
You allocate enough space for 7 cards:
(*playerhand) = (cards *) malloc(7*sizeof(cards));
Then you copy seven cards into *playerhand+5:
memcpy(*playerhand+5,player,7*sizeof(cards));
So you get a buffer overflow. *playerhand+5 is the same as &(*playerhand)[5]. So you're copying 7 cards into the fifth place in an array with enough capacity for 7 cards.
That's one problem of using magic number. I can't guess what this "5" means if you don't give it a name (nor why 7 cards).
Actually the problem is not that you're copying TO thishands but that you are copying FROM thishands. You allocated 5*sizeof(cards) to thishands,but when calling handget, you're calling it with handget(&playerhands,thishand,6);. The last argument is 6, and therein lies your problem.
Get rid of all these magic numbers. Instead make them (and this is strictly because you seem to be using C not C++) #defines. That way you have a consistent number to work with.

Resources