这是我在C
获取认证的讲座中遇到的挑战。我使用mingwin gcc编译器的代码块在64位系统上运行。
完成的程序将是一个tic tac toe游戏,但在我尝试的所有内容中,我无法得到此警告消失,这似乎也不会让我为程序创建的函数返回我需要的值。在我的搜索中似乎没有太多关于这个问题,至少似乎没有任何东西可以解决这个问题。我希望有一个简单的编码方式可以让像我这样的初学者失踪。我正在努力保持我编写的代码简单易懂,直到我掌握更高级的技术。
这是我到目前为止的编码,我的问题是函数First_Move
。在该函数中,它向我发出关于返回making an integer from a pointer with out a cast
的警告但是当我提供演员时它仍然没有给出关于哪个玩家正确猜到它的所需返回值,表明该玩家是第一个去的人。该功能简单地假设将玩家猜测与随机生成的数字进行比较,并将玩家的名字返回到主要进行第一步的位置。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int First_Move(int g1, int g2, char p1[10], char p2[10]);
int main()
{
char player1[10];
char player2[10];
int p1g;
int p2g;
char firstMove;
/* The following printf(), scanf() and system() functions asks for, takes in, then stores player 1's name and then clears the
screen for the next player.
*/
printf("Player 1 please enter your name?\n");
scanf("%s", player1);
system("cls");
/* The following printf(), scanf() and system() functions asks for, takes in, then stores player 2's name and then clears the
screen for the next function to display text.
*/
printf("Player 2 please enter your name?\n");
scanf("%s", player2);
system("cls");
printf("To determine who goes first pick a number between 1 and 10.\n Who ever guesses correctly or is closest goes first.\n");
printf("%s, enter your guess now.", player1);
scanf("%i", &p1g);
printf("%s, enter your guess now.", player2);
scanf("%i", &p2g);
First_Move(p1g, p2g, player1, player2);
firstMove=First_Move(p1g, p2g, player1, player2);
printf("The player who guessed or was closest to the number was: %c\n", firstMove);
system("cls"),
return 0;
}
int First_Move(int g1, int g2, char p1[10], char p2[10])
{
long int time_t t ;
int numberToGuess;
int g1Difference;
int g2Difference;
srand((unsigned)time(&t)); //This will seed my random number generator by using the time from the machine.
numberToGuess=rand()% 11; //This calls the random number generator and stores the generated number in numberToGuess.
/*
The following printf() is to test and make sure the number generator was correctly generating random numbers and to make sure
that the function was returning the correct value to main in relation to which player had the correct guess. This will be
encapsulated in the this comment before finalization.
*/
printf("%i\n", numberToGuess);
/*
The following series of nested if else statements goes through the process of determining if one of the players guesses
is the same as the number generated and if not then proceeds to determine which number is closer to return which guess
was the closest.
*/
{
if(g1==numberToGuess)
return (char *)p1;
else
if(g2==numberToGuess)
return p2;
else
if(numberToGuess>g1)
g1Difference=numberToGuess-g1;
else
g1Difference=g1-numberToGuess;
if(numberToGuess>g2)
g2Difference=numberToGuess-g2;
else
g2Difference=g2-numberToGuess;
}
if(g1Difference<g2Difference)
return p1;
else
return p2;
}
答案 0 :(得分:1)
嗯,我看到了几件事:
system("cls"),
应该是
system("cls");
(实际上,您应该删除此调用,因为它会立即清除屏幕而您无法看到First_Move()
的结果)
和
long int time_t t;
这应该只是
time_t t;
和
First_Move(p1g, p2g, player1, player2);
firstMove = First_Move(p1g, p2g, player1, player2);
应删除对First_Move()
的第一次调用。
关于你的指针投射警告,First_Move()
应该返回int
,但你总是返回char
数组,表示哪个玩家更接近。就个人而言,我甚至都不会传递玩家名称(他们从未在函数中使用过),而只是让First_Move()
返回1
或2
,表示哪个玩家赢了。然后通过
First_Move()
if (First_Move(p1g, p2g) == 1) {
printf("The player who guessed or was closest to the number was: %s\n", player1);
} else {
printf("The player who guessed or was closest to the number was: %s\n", player2);
}
您的方法很酷,如果您的情况更复杂,但在player1
和player2
之间进行选择会很有用,我认为它比First_Move()
更加复杂。此外,如果您稍后需要在代码中知道哪个玩家获胜First_Move()
,则检查int
的值比使用字符串更有效。
除非当然,部分任务说明必须以自己的方式完成?如果是,那么我只需将First_Move()
的返回值从int
更改为char *
。
答案 1 :(得分:1)
这里有许多“需要学习的课程”,但让我们从:
开始char firstMove;
firstMove=First_Move(p1g, p2g, player1, player2); // char = int - that's bad!
然后
int First_Move(int g1, int g2, char p1[10], char p2[10])
{
...
return (char *)p1; // but we're supposed to be returning an int
所以也许“简单回答”是First_Move
应该返回char*
......