Jump to content

Arkadaslar Turbo C Veya C++ Dan Anlayanlar Yardım


subzero1903

Recommended Posts

Slm arkadaslar bnm sorunum üniversitedeki proje ödeviyle ilgili 2 gün var.Tic Tac Toe oyunun Turbo c veya c++ dilinde yazılımı , anlatımı ve akış şeması gerekmektedir.Arkadaslar acil yardım şimdiden ilgilenenlere çok teşekkür ediyorum...

Link to comment
Share on other sites

kardeş 10. sınıfta c görmüştüm ama adımı ekrana adımı zar zor yazdırabiliyordum.şimdi 11. sınıfım basic görüyorum biraz daha kolay geldi c den.

soruna gelince googleden yazdım bu kodlar çıktı belki işine yarar...

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

/* WWW tic tac toe hack */

/* cgi-bin util routines */
void unescape_url(char *url);
void plustospace(char *str);

int vals[19683]; /* 3**9 -- not all positions are accesible, however */

enum { Xwins=1, Owins=-1, Tie=2, None=0 };
enum { X=1, O=-1 };

int diff = 0, player = 0;
char board[10];

#define FHEAD "http://www.jaked.org/ttt.files/"



int board_to_int(board)
char *board;
{
int i, out =0, exp=1;

for (i=0; i<9; i++, exp *= 3) {
switch(board[i]) {
case '-': /* zero */
break;
case 'X': /* one */
out += exp * 1;
break;
case 'O': /* two */
out += exp * 2;
}
}

return out;
}



int test_won()
{
int i, flag=0;
char *b = board;

if ((b[0] == 'X' && b[1] == 'X' && b[2] == 'X') ||
(b[3] == 'X' && b[4] == 'X' && b[5] == 'X') ||
(b[6] == 'X' && b[7] == 'X' && b[8] == 'X') ||
(b[0] == 'X' && b[3] == 'X' && b[6] == 'X') ||
(b[1] == 'X' && b[4] == 'X' && b[7] == 'X') ||
(b[2] == 'X' && b[5] == 'X' && b[8] == 'X') ||
(b[0] == 'X' && b[4] == 'X' && b[8] == 'X') ||
(b[2] == 'X' && b[4] == 'X' && b[6] == 'X'))
return Xwins;

if ((b[0] == 'O' && b[1] == 'O' && b[2] == 'O') ||
(b[3] == 'O' && b[4] == 'O' && b[5] == 'O') ||
(b[6] == 'O' && b[7] == 'O' && b[8] == 'O') ||
(b[0] == 'O' && b[3] == 'O' && b[6] == 'O') ||
(b[1] == 'O' && b[4] == 'O' && b[7] == 'O') ||
(b[2] == 'O' && b[5] == 'O' && b[8] == 'O') ||
(b[0] == 'O' && b[4] == 'O' && b[8] == 'O') ||
(b[2] == 'O' && b[4] == 'O' && b[6] == 'O'))
return Owins;

/* test for tie */
for (i=0; i<9; i++) {
if (b[i] == '-')
flag=1;
}
if (!flag)
return Tie;

return None; /* game's not over */
}



int minimax(pl, depth)
int pl, depth;
{
int best, val;
int i, index;

index = board_to_int(board);
if ((vals[index] % 10) >= depth)
return vals[index] - (vals[index] % 10); /* since 0 is a valid value */
/* we don't want use the memoized value if it is shallower than
* we're allowed to go--in fact, this doesn't matter since the memoization
* goes away with each move, but otherwise it would.
*/

if (depth > diff)
return 0;

if ((val = test_won(board)) != None) {
switch(val) {
case Xwins:
case Owins:
return 1000 * val;
case Tie:
return 0;
}
}

best = -pl * 1000000;
for (i=0; i<9; i++) {
if (board[i] == '-') {
board[i] = (pl == X) ? 'X' : 'O';
val = minimax(-pl, depth+1);
if (val * pl > best * pl)
best = val;
board[i] = '-';
}
}

vals[index] = best + depth;
return best;
}



find_move()
{
int best, val, besti[9], i, bestcount = 0;
int pl = -player; /* find the computer's move, not the player's */

best = -pl * 1000000;
for (i=0; i<9; i++) {
if (board[i] == '-') {
board[i] = (pl == X) ? 'X' : 'O';
val = minimax(-pl, 1);
if (val == best) {
best = val;
besti[bestcount++] = i;
}
else if (val * pl > best * pl) {
best = val;
bestcount = 0;
besti[bestcount++] = i;
}
board[i] = '-';
}
}
i = random()%bestcount;

board[besti[i]] = (pl == X) ? 'X' : 'O';
}



output(won)
int won;
{
int i, j;

printf("<TITLE>Tic Tac Toe</TITLE>\n<center><H1>Tic Tac Toe</H1>\n");
printf("<p>You are %s.\n", (player == X) ? "X's" : "O's");
printf("<p><table border>");

for (j=0; j<3; j++) {
printf("<TR>");
for (i=0; i<3; i++) {
printf("<TD>");
if (board[j*3+i] == '-') {
if (won == None) {
board[j*3 + i] = (player == O) ? 'O' : 'X';
printf("<a href = \"ttt?d=%c&p=%c&b=%s\">",
diff+'0', (player == O) ? 'O' : 'X', board);
printf("<img src = \"%se.gif\" border=0></a>\n", FHEAD);
board[j*3 + i] = '-';
}
else {
printf("<img src = \"%se.gif\" border=0>\n", FHEAD);
}
}
else {
printf("<img src = \"%s%c.gif\" border=0>\n",
FHEAD,
board[j*3+i] == 'X' ? 'x' : 'o');
}
printf("</TD>");
}
printf("</TR>");
}

printf("</table>\n");

if (won != None) {
printf("<form method=\"get\" action=\"/~donham/ttt.html\">\n");
printf("<p>%s won.\n",
won == Tie ? "Nobody" : (won == player ? "You" : "I"));
printf("Press <input type=\"submit\" value=\"Play\"> to play again.\n");
printf("</form>\n");
}

printf("<hr><a href = \"/~donham/index.html\">Home</a> -> Tic Tac Toe\n");
printf("</center>");

}



die(arg)
char *arg;
{
printf("<title>Tic Tac Toe</title>\n<p>Error: %s\n", arg);
exit(0);
}


/* fixed up getword which takes buffer size arg */
void getword2(word, size, line, stop)
char *word, *line, stop;
int size;
{
int x = 0,y;
--size; /* leave room for NUL */
for(x=0;((line[x]) && (line[x] != stop));x++)
if (x < size)
word[x] = line[x];

if (x < size)
word[x] = '\0';
else
word[size] = '\0';

if(line[x]) ++x;
y=0;

while(line[y++] = line[x++]);
}

void process_query()
{
char *cl, *rm, buf[100], name[100];
int df = 0, pf = 0, bf = 0;
int i;

if ((rm = getenv("REQUEST_METHOD")) == 0)
die("No request method.\n");
if (strcmp(rm, "GET") != 0)
die("Script not referenced with GET method.\n");

if ((cl = getenv("QUERY_STRING")) == 0)
die("No query string.\n");

while (cl[0] != 0) {
getword2(buf, 100, cl, '&');
plustospace(buf);
unescape_url(buf);
getword2(name, 100, buf, '=');

switch (name[0]) {

case 'd':
switch (buf[0]) {
case '1':
case '2':
case '9':
diff = buf[0] - '0';
break;
default:
die("Bad query string.\n");
}
df = 1;
break;

case 'p':
switch(buf[0]) {
case 'X':
player = X;
break;
case 'O':
player = O;
break;
default:
die("Bad query string.\n");
}
pf = 1;
break;

case 'b':
strncpy(board, buf, 10);
board[10] = 0;
for (i = 0; i < 9; i++)
switch (board[i]) {
case 'O':
case 'X':
case '-':
break;
default:
die("Bad query string.\n");
}
bf = 1;
break;

default:
die("Bad query string.\n");
}
}

if (!df)
diff = 2;
if (!pf)
player = X;
if (!bf)
strcpy(board, "---------");
}



main(argc, argv)
int argc;
char **argv;
{
int won, i;

printf("Content-type: text/html\n\n");

process_query();

if (strcmp(board, "---------") == 0 && player == X) {
output(0); /* first move--empty board */
exit(0);
}
else if ((won = test_won()) != None) { /* last move ended game */
output(won);
exit(0);
}
else {
srandom(getpid());
find_move(); /* puts the move into board */
won = test_won();
output(won);
exit(0);
}
}

/* from NCSA httpd util.c */

char x2c(char *what) {
register char digit;

digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}

void unescape_url(char *url) {
register int x,y;

for(x=0,y=0;url[y];++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}

void plustospace(char *str) {
register int x;

for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}[/CODE]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...