So, I'm running into an issue with the end of this program. It is supposed to award ten points for each correctly guessed coin flip, and at the end of the 100 tosses, a percentage of correct answers for each player as well as a total score, and finally it is to declare a winner. I seem to have the percentage correct, but the output for total score is jibberish, and I don't know how to get the program to use the results to declare a winner. Help?
#include "stdafx.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int flip();
int main(void)
{
int player, side, toss, turn = 1, heads = 0, tails = 0;
int Play, wrong1, right1, wrong2, right2, r = (rand() % 2);
puts("Oh boy. My favorite game. Flip the coin. How super exciting. I apparently am little more than a childs plaything... \nEven though I am a program and thus do not have thumbs, hands, feet, skin or even a body, I will (reluctanly) play your silly little game, and 'flip a coin' 100 times.\nI hope you know, there are a lot better things I could be doing right now...\nReal quick, here's a list of things I could be doing right now.\n--Advance scientific research on globular clusters in the Milky Way Galaxy\n---Find the cure for cancer\n----Calculate the rate of deforestation in the Amazon\n-----Create the next generation of low emmission motor vehicles to lower the amount of CO2 in the atmosphere.\nBut, no. Let's play 'Flip the coin'. I couldn't possibly be more ecstatic, than if I woke this morning to see that I was on fire. While stapled to a wall. In New Jersey. With Kim Kardashian holding the fire extinguisher that is actually a can of hairspray... Yeah.\n");
puts("Are you player 1 or player 2?\n");//question to prompt user response
scanf_s("%d", &player);//how many players input
puts("\nSo, let's flip a coin, because apparently this game is the 'bees-nees' of the tech world... Enter 1 for heads, or 0 for tails.");
srand((unsigned)time(NULL));// seed random function with current time
for (toss = 1; toss <= 100; toss++)
{ //number of runs/tosses
if ( player=1)
if (turn == 1) // start of 2 player gaming
printf("\nPlayer 1, flip the coin. Player 2, take a guess.\n");// player 1 flip
else
printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n"); // player 2 flip
if (turn == 1)
Play = 1, turn = 0;
else
Play = 2, turn = 1;
printf("\nPlayer 1 toss the coin and guess the side\n");
int flipped;
scanf_s("\n %d", &side);// coin guess function
printf("\nThe coin came up %d\n", flip());
if (side == flip() && Play == 1)
right1++;
else
wrong1++;
if (side == flip() && Play == 2)
right2--;
else
wrong2++;
if (flip() == 1)
heads++;
else
tails++;
}
printf("heads was flipped %d times\n", heads);
printf("tails was flipped %d times\n", tails);
printf("Player 1 score %d \n", (right1 * 10) - (wrong1 * 5));// not working quite right player 1 eqation 1 //functions on the next 4 lines for score and confidence
printf("Player 1 confidence %d %'\n", (right1 * 2));//percent confidence player 1 Equation 2
printf("Player 2 score %d \n", (right2 * 10) - (wrong2 * 5));// not working quite right player 2 eqation 1
printf("Player 2 confidence %d %'\n", (right1 * 2));//percent confidence player 2 Equation 2
return 0;
}
int flip()// Coin flip function
{
int i = rand() % 2;
if (i == 0)
return 0;
else
return 1;
}
I strongly suggest properly formatting nested if statements with brackets and indentation.
if ( player=1)
if (turn == 1) // start of 2 player gaming
printf("\nPlayer 1, flip the coin. Player 2, take a guess.\n");// player 1 flip
else
printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n"); // player 2 flip
is much more difficult to read than
if ( player=1) {
if (turn == 1) { // start of 2 player gaming
printf("\nPlayer 1, flip the coin. Player 2, take a guess.\n");// player 1 flip
} else {
printf("\nPlayer 2, it's your turn to flip the coin, Player 1 guess heads or tails.\n"); // player 2 flip
}
}
And yes, as mentioned in the comments
if ( player=1) {
should be
if (player == 1) {
And
if (turn == 1)
Play = 1, turn = 0;
else
Play = 2, turn = 1;
should be
if (turn == 1) {
Play = 1;
turn = 0;
} else {
Play = 2;
turn = 1;
}
And you are also using right1, wrong1, right2, and wrong2 without initializing them.
And finally, in printf(), %' should be %% if you are trying to print a percent sign.
Related
i am trying to simulate monty hall problem. i didn't realized any problem but i recieve approximately %50 %50 output. i know that there are explanations but i couldnt understand these
please help me
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define win 1
#define lose 0
#define yes 1
#define no 0
int main(){
//i assume that prize is behind the first door
srand(time(NULL));
int lose_counter = 0;
int win_counter = 0;
for(int i = 1;i <= 10000;i++){
int game_status;
int chosen_door = rand() % 3 + 1;
int choice;
//first step that i chose a door
if(chosen_door == 1){
game_status = win;
}
else if(chosen_door == 2){
game_status = lose;
}
else if(chosen_door == 3){
game_status = lose;
}
//host says "do you want to change your door"
choice = rand() % 2;
if(choice == yes){
if(chosen_door == 1 ){//this is the case i have chosen
//first door and change it after question
game_status = lose;
}
if(chosen_door == 2 ){
game_status = win;
}
if(chosen_door == 3 ){
game_status = win;
}
}
if(game_status == win){
win_counter++;
}
else if(game_status == lose){
lose_counter++;
}
}
printf("win: %d\nlose: %d\n",win_counter,lose_counter);
return 0;
}
The chances to win when not switching the doors are 1/3.
If you switch the chances increase to 2/3.
Your code takes samples of 50% switching and 50% not switching, hence the average of 1/3 and 2/3 comes up as 0.5.
The odds of winning are 2/3 if you switch.
Using
choice = 1; // Switch
results in win in about 2/3 of your trials as expected.
The odds of winning are 1/3 if you don't switch.
Using
choice = 0; // Keep
results in win in about 1/3 of your trials as expected.
If you randomly decide to switch or keep, the odds of winning are
( 1/2 * 2/3 ) + ( 1/2 * 1/3 ) = 1/2
Using
choice = rand() % 2;
results in win in about 1/2 of your trials as expected.
Fixed:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
// I assume that prize is behind the first door
srand(time(NULL));
unsigned N = 10000;
unsigned win_if_keep_counter = 0;
unsigned win_if_switch_counter = 0;
for (unsigned i=N; i--; ) {
int chosen_door = rand() % 3 + 1;
if (chosen_door == 1) {
// Monte open door 2 or 3.
// You win if you keep door 1.
++win_if_keep_counter;
}
else if (chosen_door == 2) {
// Monte open door 3.
// You win if you switch to door 1.
++win_if_switch_counter;
}
else if (chosen_door == 3) {
// Monte open door 2.
// You win if you switch to door 1.
++win_if_switch_counter;
}
}
printf("win if keep: %.1f%%\n", ((double)win_if_keep_counter)/N*100);
printf("win if switch: %.1f%%\n", ((double)win_if_switch_counter)/N*100);
}
So, as a task for C, I need to create a card game. Naturally, shuffling be an important part of the code.
I designed the deck to hold cards 52 cards (no jokers), which were identified as integers:
Hundreds would represent the suit, while the tens and ones would represent the face.
I started out by plugging in these numbers into the deck in order first, then shuffled it with a code that basically takes the number from two random indexes in the array and swaps them. This swapping loops for a million times.
However, for some odd reason, after shuffling, the number 0 (Ace of Spades) would always appear twice after shuffling. I wrote a debugging code that would alert when two elements are identical in the array, and it is always 0.
I look through my code over and over, and I can't seem to find the reason why. Is there a characteristic of C that I'm not noticing?
Code:
/* Suit and Face legend:
Suits
000 - Spade
100 - Clover
200 - Diamond
300 - Heart
Faces
0 - Ace
1 - 2
2 - 3
3 - 4
4 - 5
5 - 6
6 - 7
7 - 8
8 - 9
9 - 10
10 - Jack
11 - Queen
12 - King
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
#define MONEY 500
void debugPrintDeck();
static int deck[CARDS];//deck used in game
int main(){
//Variables
int cnt = -1; //Keeps track of how many cards there are in the deck, as well as the nth card on top. -1 if there's no card in the deck. 0 indexing.
srand(time(0));
puts("Stacking deck in order...");
//PLACING CARDS IN ORDER IN DECK (First Step)
for(int crd = -1; crd < 312;){//Starting from the first Spade (000) to Heart (300)
crd++; //Starts with Ace.
cnt++; //indicating a card has been added to the deck.
deck[cnt] = crd;
if(crd == 12 || crd == 112 || crd == 212 || crd == 312) crd += 87;
}
debugPrintDeck();
puts("\nShuffling in progress...");
//SHUFFLING CODE (Second Step)
//Includes code that will print the values that switch.
//Also include code that will terminate the program when it detects identical numbers in different indexes.
for (int shfl = 1; shfl < 1000000; shfl++){
int rng1 = -1; //initialize to a value not in the deck.
int rng2 = -1;
int temp1 = -1;
int temp2 = -1;
do{
rng1 = rand() % 53;
rng2 = rand() % 53;
} while (rng1 == rng2); //ensures the values won't randomize into the same position.
temp1 = deck[rng1];
temp2 = deck[rng2];
printf("\ntemp1 at deck[%d]= %d",rng1, temp1); //debug
printf(" temp2 at deck[%d]=%d",rng2, temp2); //debug
deck[rng2] = temp1;
deck[rng1] = temp2;
if (deck[rng2] == deck[rng1]) { //debug
printf("\nALERT ALERT ALERT: rng1 #%d is %003d, rng2 #%d is %003d", rng1, deck[rng1], rng2, deck[rng2]);
exit(0);
}
}
debugPrintDeck();
}
void debugPrintDeck(){
puts("\nThe following is the full stack of cards from end to end.");
for (int card = 0; card < 52; card++){
printf("\ncard #%d = %003d", card, deck[card]);
}
}
Note: If you want the program to complete the shuffling and terminate properly, just remove line 82.
This is my first post so don't go rough on me if I happened to skip something I should have mentioned.
I am working on a program that simulates a tennis championship, and the outcome of an individual match via a rand() function to distribute the points among the 2 players currently in said match.
I have scanned the program and did my best to fix the issues but there is still a problem I can not fix, it is the function that determines the outcome of the matches. The problem is that the function returns strange values, nothing they should look like.
I first thought there is a problem with the declaration of the variables and the way I did operations with them but that did not seem to be the case, so I ran out of ideas and come to you.
Here is the code:
jucatori meci(jucatori jucator1,jucatori jucator2 )
{
int u =0; //scor jucator 1
int o=0; //scor jucator 2
int p; //aux j1
int l; //aux j2
int i;
do
{
//first loop, where player 1 serves
p= rand() % jucator1.sansaServire; //generate a number for player1
l= rand() % jucator2.sansaPrimire; //generate a number for player2
if(l>p)
{
o=o+1;
do
{
//going into the second loop, in this loop, here, player 2 serves
p = rand() % jucator1.sansaPrimire;
l = rand() % jucator2.sansaServire;
if(p>l) u = u+1;
if(l>p) o = o+1;
else break; //iese din bucla 2, juc1 va servi iarasi
}while(l>p);
}
else if(p>l) u = u+1;
}while(u + o < 51); //inchidem prima bucla
if(u > o && u - o > 10)
{
jucator1.meciurijucate++;
jucator2.descalificat = 1;
jucator1.scor1 = u;
jucator1.scor2 = o;
return jucator1;
} //return player 2, add 1 to the number of matches played and mark player 1 as out of the game
else if(o > u && o - u > 10)
{
jucator2.meciurijucate++;
jucator1.descalificat = 1;
jucator2.scor1 = o;
jucator2.scor1 = u;
return jucator2;
}//return player 1, add 1 to the number of matches played and mark player 2 as out of the game
else return meci(jucator1,jucator2); //call function again if the match is a draw
}
I will provide the full program if needed, just thinking that it would be pretty bulky to post the whole thing.
I want to make a game; say there are 50 turns and up to 4 players.
How should the code manage the turn by turn?
For 2 player I think it is that:
if (nbr_gamers == 2)
{
if ((turn % 2) == 0)
player = 1;
else
player = 0;
}
where the turn is the position of turn.
Is that about right?
Try this :
player = turn % nbr_gamers;
if (turn == tick) {
/*first player*/
Form1->Label1->Caption = "X pyr";
fields[row][kol] = 1;
Form1->BitBtn1->Glyph->LoadFromFile("tick.bmp");
turn = tack;
}
else {
do {
//random
row = rand() % 3;
kol = rand() % 3;
}
while (fields[row][kol] == 0);
/*cpu*/
Form1->Label1->Caption = "CPU";
fields[row][kol] = 2;
Form1->BitBtn1->Glyph->LoadFromFile("tack.bmp");
turn = tick;
}
}
The main problem is that when I make my move, computer just clicks on first element and after every next move it does the same.
Computer just uses first TicTacToe game board square.
If i understand correctly the fields variable contain the board with 0 for unoccupied cell, 1 for human player, 2 for CPU.
In this case the terminal condition of the while is wrong while (fields[row][kol] == 0);, you must loop when the cell is occupied (trying to search for free cells).
do {
//random
row = rand() % 3;
kol = rand() % 3;
}
while (fields[row][kol] != 0);
Note: you are initializing all elements of fields to 0, that don't appear in the code.
CPU player loops until it finds a row and col value which is not equal to 0. do-while loop below loops if fields[row][col] is equal to 0 meaning after the exit fields[row][col] will be different than 0.
do {
...
} while(fields[row][col] == 0)
// fields[row][col] is different than 0 here
In your case field value not equal to 0 means a square already used by human or computer, so computer does the same move every time.