ABA


"לילה לבן..... הכל בשביל כ-450 שורות קוד"
גירסת הדפסה        
קבוצות דיון פיתוח, תיכנות ובניית אתרים נושא #14215 מנהל    סגן המנהל    מפקח   Winner    צל"ש   מומחה  
אשכול מספר 14215
idan192

דרג אמינות חבר זה
   07:48   18.07.07   
אל הפורום  
  לילה לבן..... הכל בשביל כ-450 שורות קוד  
 
   עבר עריכה לאחרונה בתאריך 24.07.07 בשעה 20:23 על-ידי Nesher (מנהל הפורום)
 
שעושות את זה:


/*-------------------------------------------------------------------------
Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>

/*=====
Constants and definitions:
======*/
#define N 10
#define BLANK 0
typedef enum {player_x='x', player_o='o'} Players;
typedef enum {false, true} bool; // You may need to delete this in compilers
// other than Dev-C++

/*=====
Forward declarations:
======*/
int number_of_flips(Players player, int row, int col);
void switch_players(void);
void i_beat_you(int row, int col);
int game_type(void);
void ai(void);
void crash(void);
void print_board(void);
void score(void);
void statistics(void);
void hi(int method,int player_id);

/*=====
Global variables:
======*/

char board[N][N] = {BLANK};
int chose_y, chose_x, y, x, player = 'x', opposite = 'o';
int player_id, x_counter, o_counter, method, changer = 0;

/*-------------------------------------------------------------------------
The main program. (plays Riversi with a computer friend or a human)
-------------------------------------------------------------------------*/

int main()
{
int check, end=2;

board[N/2][N/2] = player_x;
board[N/2][N/2+1] = player_o;
board[N/2+1][N/2] = player_o;
board[N/2+1][N/2+1] = player_x;

method = game_type();

if((method%2)!=0)
player_id=1;
if(method == 3)
player_id=2;

while(1)
{
player_id=(player_id%2)+1;

score();
system("cls");

if (method == 4)
{
ai();
continue;
}

while(1)
{
player_id=(player_id%2)+1;
score();
system("cls");

switch (method)
{
case 1:
{
hi(method,player_id%2+1);
hi(method,player_id);
break;
}
case 2:
{
ai();
system("cls");
hi(method,player_id+1);
break;
}
case 3:
{
hi(method,player_id);
ai();
break;
}
case 4:{
ai();
ai();
break;
}
}
}
}
system("pause");
return 0;
}

/*=====
Function implementation:
======*/
/*-------------------------------------------------------------------------
Calculates the number of flips that will result by locating a coin in
board[row][col] by player.
-------------------------------------------------------------------------*/

int number_of_flips(Players player, int row, int col)
{
static int x_dir[8] = {1, 1, 0, -1, -1, -1, 0, 1};
static int y_dir[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int directions[8] = {1, 1, 1, 1, 1, 1, 1, 1}, dirs_left = 8;
int radius;
int tot_flips = 0;
int d;

if((col <= 0) || (row <= 0) || (col > N) || (row > N) || (board[row][col] != BLANK))
return 0;

for(radius=1; dirs_left>0; radius++)
{
for(d=0; d<8; d++)
{
int x = col+radius*x_dir[d], y = row+radius*y_dir[d];
if(!directions[d])
continue;
if((x <= 0) || (x > N) || (y <= 0) || (y > N) || (board[y][x] == 0) ||
((board[y][x] == player) && (radius == 1)))
{
directions[d] = 0;
dirs_left--;
continue;
}
if((board[y][x] == player) && (radius > 1))
{
tot_flips += (radius-1);
directions[d] = 0;
dirs_left--;
continue;
}
}
}
return (tot_flips);
}

/*-------------------------------------------------------------------------
Prompts the user for the desired game type, validates it
-------------------------------------------------------------------------*/

int game_type()
{
double selection;
printf("Reversi:\n====\n");
printf("Every player at his turn must place a piece with\n");
printf("his side up on the board, in such a position that\n");
printf("there exists at least one straight (horizontal,\n");
printf("vertical, or diagonal) line between the NEW piece\n");
printf("and another of his pieces, with one or more continuous\n");
printf("opponent-pieces between them.\n\n");

printf("Game Types:\n1) x-Human, o-Human\n2) x-Computer, o-Human\n");
printf("3) x-Human, o-Computer\n4) x-Computer, o-Computer\n\n");
while(1)
{
printf("Enter game type (1,2,3,4): ");
if((scanf("%lf", &selection) != 1) ||
(selection != (int)selection) || (selection < 1 || selection > 4))
{
printf("Wrong input\n");
scanf("%c");
}
else
break;
}
print_board();
return (int)selection;
}

/*-------------------------------------------------------------------------
Flips the coins on the board
-------------------------------------------------------------------------*/

void i_beat_you(int row, int col)
{
int y_dir[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int x_dir[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int x, y, radius, i, a, b;

for(i=0; i<8; ++i)
{
for(radius=1; radius<=8; ++radius)
{
y = row + (y_dir[i])*radius;
x = col + (x_dir[i])*radius;
if(((x>0)&&(x<=N)) && ((y>0)&&(y<=N)))
{
if(board[y][x] == opposite)
{
continue;
}
else if(board[y][x] == player)
{
break;
}
else if(board[y][x] == BLANK)
{
x_dir[i] = -2;
y_dir[i] = -2;
break;
}
}
else
break;

}
}
for(i=0; i<8; ++i)
{
for(radius=1; radius<=N; ++radius)
{
y = row + (y_dir[i])*radius;
x = col + (x_dir[i])*radius;
a = row + (y_dir[i])*(radius+1);
b = col + (x_dir[i])*(radius+1);
if((x_dir[i] == -2)||(y_dir[i] == -2)||((x<=0)||(x>N)) || ((y<=0)||(y>N)))
break;
if((board[y][x] == opposite)&&(board[a][b] != BLANK))
{
board[y][x] = player;
}
}
}
switch_players();
}

/*-------------------------------------------------------------------------
Crashes the program when input is invalid
-------------------------------------------------------------------------*/

void crash()
{
printf("Input failure - ending.\n");
system("pause");
exit(1);
}

/*-------------------------------------------------------------------------
Changes back & forth between the players, according tho who'se turn it is
-------------------------------------------------------------------------*/

void switch_players(void)
{
if (player == player_x)
{
player = player_o;
opposite = player_x;
}
else
{
player = player_x;
opposite = player_o;
}
}

/*-----------------------------------------------------------------------------
The function is a computer player which plays a turn
------------------------------------------------------------------------------*/

void ai(void)
{
int col, row = 1, mid = 0, bkup_row, bkup_col, radius=0;
chose_y = 0;
chose_x = 0;
for (col = 1; row <= N; ++col)
{
radius = number_of_flips(player, row, col);
if (mid <= radius)
{
mid = radius;
bkup_row = row;
bkup_col = col;
}
if (col==N)
{
col = 0;
row++;
}
}
chose_y = bkup_row;
chose_x = bkup_col;
board[bkup_row][bkup_col] = player;
print_board();
printf("computer play: row %d column %d \n", bkup_row,bkup_col);
i_beat_you(bkup_row, bkup_col);
}

/*-------------------------------------------------------------------------
Prints the current board status
-------------------------------------------------------------------------*/

void print_board(void)
{
int i,j,k;

putchar(' ');
for(i=0;i<N;i++)
printf(" %d",i+1);

printf(" \n");
for(i=0;i<N;i++)
{
printf(" +");
for(j=1; j<((N*4)); j++)
putchar('-');
printf("+\n%d |",i+1);

for (k=0;k<N;k++)
{
printf(" %c |",board[i+1][k+1]);
}
putchar('\n');
}
printf(" +");
for(j=1;j<((N*4));j++)
putchar('-');
printf("+\n");
}

/*-------------------------------------------------------------------------
Keeps track of the exes and o's on the board
-------------------------------------------------------------------------*/

void score(void)
{
int i, j;
x_counter = 0;
o_counter = 0;
for(i=1; i<=N; ++i)
{
for(j=1; j<=N; ++j)
{
if(board[i][j] == player_x)
x_counter++;
if(board[i][j] == player_o)
o_counter++;
}
}
if ((x_counter == 0)||(o_counter == 0)||(x_counter + o_counter == N*N))
statistics();
}

/*-------------------------------------------------------------------------
Displays the end game statistics
-------------------------------------------------------------------------*/

void statistics(void)
{
int who_won;
if(x_counter > o_counter)
who_won = 1;
else
who_won = 2;
printf("\n\nPlayer 1: %d, Player 2: %d\n", x_counter, o_counter);
printf("Player %d won.\n", who_won);
system("pause");
exit(0);
}

/*-------------------------------------------------------------------------
Plays the human players turn(flips the coins)
-------------------------------------------------------------------------*/

void hi(int method,int player_id)
{
int check;
print_board();
while(1)
{
if(method == 1)
{
player_id=(player_id%2)+1-changer;
if(changer == 1)
changer = 0;
if (player_id == 0)
{
changer == 1;
player_id = 2;
}
}
if(method == 2)
player_id = 2;
if(method == 3)
player_id = 1;
do
{
printf("\n\nPlayer %d (%c) enter row (1-8): ",player_id, player);
if(scanf("%d",&chose_y) != 1)
crash();
} while ( (chose_y < 0 || chose_y > 8));

do
{
printf("Player %d (%c) enter column (1-8): ",player_id,player);
if(scanf("%d",&chose_x) != 1)
crash();
} while ( (chose_x < 0 || chose_x > 8));
check = number_of_flips(player, chose_y, chose_x);
if (check == 0)
{
printf("Your move does not flip any coins, enter another move.\n");
changer = 1;
continue;
}
else
{
y=chose_y;
x=chose_x;
board[y][x] = player;
i_beat_you(y, x);
system("cls");
print_board();
if (method == 2 || method == 3)
break;
else
continue;
}
}
}

/******
End of file ex4.c
***/



                                שתף        
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

  האשכול     מחבר     תאריך כתיבה     מספר  
  תעשה שזה ינקה את המסך כל סיבוב וידפיס את הלוח מחדש.. bloodkiller 18.07.07 08:15 1
     זה בעייתי, כי לפעמים ה-ai משחק ולפעמים אתה. idan192 18.07.07 15:22 2
         תכלס דומה למה שאני וחבר עשינו (גם ביום \ יומיים) bloodkiller 25.07.07 21:40 9
             כן, הקטע שלנו זה צריך לבוא יחד עם שיעורים idan192 27.07.07 00:44 10
                 כרגע לא התחלתי אבל אולי אני יעשה בקרוב כי משעמם לי רצח bloodkiller 27.07.07 11:37 11
  הקטע שלא רואים כל פעם את הלוח די מקשה.. asco88  20.07.07 15:16 3
     למה לא רואים? תאפס את ההגדרת של חלון הדוס שלך. idan192 20.07.07 21:13 4
         צודק.. asco88  23.07.07 19:37 5
             אין בעיה. בכל מקרה זו גירסא עם 5-6 באגים. לא קריטי. idan192 24.07.07 01:06 6
  סידרתי לך את הקוד... Nesher  24.07.07 20:24 7
     מגניב, תודה :) וכן... בהחלט לילה לבן ומרגיז. idan192 25.07.07 00:24 8

       
bloodkiller
חבר מתאריך 23.9.03
295 הודעות, דרג אמינות חבר זה
   08:15   18.07.07   
אל הפורום  
  1. תעשה שזה ינקה את המסך כל סיבוב וידפיס את הלוח מחדש..  
בתגובה להודעה מספר 0
 
  



                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
idan192

דרג אמינות חבר זה
   15:22   18.07.07   
אל הפורום  
  2. זה בעייתי, כי לפעמים ה-ai משחק ולפעמים אתה.  
בתגובה להודעה מספר 1
 
   ערכתי לאחרונה בתאריך 18.07.07 בשעה 15:45 בברכה, idan192
 


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
bloodkiller
חבר מתאריך 23.9.03
295 הודעות, דרג אמינות חבר זה
   21:40   25.07.07   
אל הפורום  
  9. תכלס דומה למה שאני וחבר עשינו (גם ביום \ יומיים)  
בתגובה להודעה מספר 2
 
   איקס עיגול שפורסם פה מלפני שנה :]


קיצר אממ זה לא צריך להיות בעיה אם המחשב משחק או לא כי התוכנית סה"כ צריכה לרוץ בסגנון הבא:

1. כל עוד אין מנצח
1.1 אם תור שחקן א'
1.1.1 בצע - קבלת קלט
1.2 אם תור שחקן ב'
1.2.1 בצע - קבלת קלט
1.3 נקה מסך
1.4 צייר לוח
1.5 בדוק אם יש מנצח
1.5.1 אם כן ... עשה משהו לסיום הלולאה

בפונקצית קבלת קלט אתה בודק תקינות וכו' ויש לך מטריצה שמייצגת את המיקום של כל אחד ואחד מהריבועים כאשר "-" זה ריק וסימון אחר זה מלא.

-----

גרמת לי לחשק לכתוב תוכנית SOS אז אולי אני יוציא את זה לפועל בקרוב :]



                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
idan192

דרג אמינות חבר זה
   00:44   27.07.07   
אל הפורום  
  10. כן, הקטע שלנו זה צריך לבוא יחד עם שיעורים  
בתגובה להודעה מספר 9
 
   בספרתיות + חדו"א + אלגברה, שכל אחד לוקח 8 שעות לפחות (ואני לא מגזים).

ותהנה מחכה לתוצאה פה!


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
bloodkiller
חבר מתאריך 23.9.03
295 הודעות, דרג אמינות חבר זה
   11:37   27.07.07   
אל הפורום  
  11. כרגע לא התחלתי אבל אולי אני יעשה בקרוב כי משעמם לי רצח  
בתגובה להודעה מספר 10
 
  



                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
asco88 
חבר מתאריך 17.6.04
26757 הודעות, דרג אמינות חבר זה
   15:16   20.07.07   
אל הפורום  
  3. הקטע שלא רואים כל פעם את הלוח די מקשה..  
בתגובה להודעה מספר 0
 


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
idan192

דרג אמינות חבר זה
   21:13   20.07.07   
אל הפורום  
  4. למה לא רואים? תאפס את ההגדרת של חלון הדוס שלך.  
בתגובה להודעה מספר 3
 
  


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
asco88 
חבר מתאריך 17.6.04
26757 הודעות, דרג אמינות חבר זה
   19:37   23.07.07   
אל הפורום  
  5. צודק..  
בתגובה להודעה מספר 4
 
מצטער..


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
idan192

דרג אמינות חבר זה
   01:06   24.07.07   
אל הפורום  
  6. אין בעיה. בכל מקרה זו גירסא עם 5-6 באגים. לא קריטי.  
בתגובה להודעה מספר 5
 
  


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
Nesher  לחץ כאן להצגת דירוג המשתמש
חבר מתאריך 2.7.02
2 הודעות, 24 פידבק, 43 נקודות
   20:24   24.07.07   
אל הפורום  
  7. סידרתי לך את הקוד...  
בתגובה להודעה מספר 0
 
ויפה שעשיתי משהו בלילה אחד
יש כאלה שיכולים לשבור ראש ימים ולא להצליח....


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד
idan192

דרג אמינות חבר זה
   00:24   25.07.07   
אל הפורום  
  8. מגניב, תודה :) וכן... בהחלט לילה לבן ומרגיז.  
בתגובה להודעה מספר 7
 
  


                                                         (ניהול: מחק תגובה)
מכתב זה והנלווה אליו, על אחריות ועל דעת הכותב בלבד

תגובה מהירה  למכתב מספר: 
 
___________________________________________________________________

___________________________________________________________________
למנהלים:  נעל | תייק בארכיון | מחק | העבר לפורום אחר | מחק תגובות | עגן אשכול
       



© כל הזכויות שמורות ל-רוטר.נט בע"מ rotter.net