הרשם שאלות ותשובות רשימת חברים לוח שנה הודעות מהיום

חזור   הוסטס - פורום אחסון האתרים הגדול בישראל > עיצוב גראפי, תכנות על כל שפותיו וקידום ושיווק אתרים > פורום תיכנות

   
|!|

השב
 
כלים לאשכול תצורת הצגה
ישן 03-05-08, 18:49   # 1
BBman
חבר מתקדם
 
מיני פרופיל
תאריך הצטרפות: Oct 2005
גיל: 31
הודעות: 608
שלח הודעה באמצעות MSN אל BBman

BBman לא מחובר  

איקס עיגול - כשהשיעמום עולה על גדותיו :) [שפת C]

למי שאי פעם תהה איך עושים דבר בסגנון הזה, מוזמן להתסכל על הקוד הקצרצר שכתבתי.
זהו משחק איקס עיגול פשוט.
יש אפשרות לשני שחקנים ולמשחק נגד מחשב (המחשב לא ממש חושב, הוא יותר מנחש, למי יש כוח לתכנת חשיבה מלאכותית :P? )

תהנו!!!
PHP קוד:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#define n 3
#define nl 30

void ipus(char b[n][n])
{
    
int i,j;

    for(
i=0;i<n;i++)
    {
        for(
j=0;j<n;j++)
        {
                    
//45 is '-'
            
b[i][j] =45;
        }             
    }
}

int isavail(int x,int y,char b[n][n])
{
    
//45 IS '-'
    
if(b[x][y]!= 45)
        return 
0;
    return 
1;
}

void fill(int x,int y,char c,char b[n][n])
{
    
b[x][y] = c;
        
cout<<endl;
}

void comp_pos(int *x,int *y,char b[n][n])
{
    
randomize();

        
int n1,n2;

    do
    {
        
n1 random(n);
        
n2 random(n);
    }
    while(
b[n1][n2]!=45);

    *
n1;
        *
n2;

}
void showboard(char b[n][n],int type,char name1[nl],char name2[nl])
{
    
clrscr();
    
int i,j;

        
cout<<"  ";
    for(
i=0;i<n;i++)
        
cout<<i<<"  ";
    
cout<<endl;

    for(
i=0;i<n;i++)
    {
            
cout<<i;
        for(
j=0;j<n;j++)
        {
            
cout<<"["<<b[i][j]<<"]";
        }
                
cout<<"\n";
    }

    
cout<<"\nX - "<<name1;

        if(
type==1)
         
cout<<"\nO - "<<name2<<endl;
    else
         
cout<<"\nO - computer\n";
}

int alachsonim(char b[n][n],char c)
{
    
int i,j,m=1;

    
/*alachson rashi*/
    
for(i=0;i<n;i++)
    {
        if(
b[i][i]!=c)
            
0;      
    }

    
/*mishni*/
       
if(!m)
       {
     
=1;
     for(
i=0;i<n;i++)
     {
        for(
j=0;j<n;j++)
        {
            if(
i+j==n-1)
            {
                        
                if(
b[i][j]!=c)
                    
m=0;
            }
        }
           }
       }

       return 
m;
}

int rows(char b[n][n],char c)
{
    
int i,j,m=0;

    for(
i=0;i<3;i++)
    {
        for(
j=0;j<n;j++)
        {
            if(
b[i][j]==c)
                
m++;
        }
        if(
m!=n)
            
m=0;
        else
                    return 
1;
    }

    return 
0;
}

int cols(char b[n][n],char c)
{
    
int i,j,m=0;

    for(
i=0;i<n;i++)
    {
        for(
j=0;j<n;j++)
        {
            if(
b[j][i]==c)
                
m++;
        }

        if(
m!=n)
            
m=0;
        else
            return 
1;
                        
    }

    return 
0;
}

int isnotfull(char b[n][n])
{
    
int i,j;

    for(
i=0;i<n;i++)
    {
        for(
j=0;j<n;j++)
        {
            if(
b[i][j]==45)
                return 
1;
        }
    }

        return 
0;
}
int wincheck(char b[n][n],int c)
{
    if(
alachsonim(b,c) || rows(b,c) || cols(b,c))
        return 
1;
    else
        return 
0;

}
void startgame(int *type,char name1[nl],char name2[nl])
{
    
cout<<"Enter type of game(1=p vs p;2 = p vs comp): ";
    
cin>>*type;


    
cout<<"Player 1(X), enter your name :";
    
cin>>name1;

    if(*
type==1)
        {
        
cout<<"Player 2(O), enter your name :";
        
cin>>name2;
        }

}
void declarewinner(char b[n][n],char c,char name1[nl],char name2[nl],int type)
{

        if(
alachsonim(b,c))
            
cout<<"\n[oblique] ";
        else if(
rows(b,c))
            
cout<<"\n[rows] ";
        else
            
cout<<"[columns] ";

        
cout<<"THE WINNER IS :";

        if(
c=='X')
            
cout<<name1;
        else
        {
                    if(
type==1)
                
cout<<name2;
            else
                
cout<<"computer";
                }
}
void switchplayer(int m,int e,char *c)
{
        if(!
&& e)
                {
            if(*
c=='X')
                *
'O';
            else
                *
'X';
        }
}
void main()
{
    
char b[n][n];
    
char c='X',i;
        
char name1[nl],name2[nl];
    
int x,y,m=0,e=1,win=0;
    
int isfull;
    
int type;

    
startgame(&type,name1,name2);
    
ipus(b);
    
showboard(b,type,name1,name2);

    while(
isnotfull(b))
    {
        
cout<<"\nNOW PLAYING:";

            if(
c=='X')
                
cout<<name1;
            else
            {
                            if(
type==1)
                    
cout<<name2;
                else
                    
cout<<"computer";
                        }

            if(
type==|| (type==&& c=='X'))
            {
                
cout<<"\nenter position :";
                
cin>>x>>y;
            }
            else
                
comp_pos(&x,&y,b);

        if(
isavail(x,y,b))
        {
            
fill(x,y,c,b);
            
showboard(b,type,name1,name2);

            if(
wincheck(b,c))
            {            
                            
declarewinner(b,c,name1,name2,type);
                                
win 1;
                
m=1;
                                break;
            }
            
e=1;

        }
        else
                {
            
cout<<"\nTHIS SQUARE IS NOT AVAILABLE!\n\a";
            
0;
                }

        
switchplayer(m,e,&c);
    }

    if(!
win)
        
cout<<"\nTHAT'S A DRAW!";

            

  Reply With Quote
ישן 04-05-08, 17:43   # 2
Daniel
אחראי פורום
 
מיני פרופיל
תאריך הצטרפות: Mar 2007
הודעות: 2,875

Daniel לא מחובר  

יהיה הרבה יותר נחמד ומאתגר אם תכין דבר כזה - אך שהוא ילמד מההפסדים שלו.
  Reply With Quote
ישן 04-05-08, 18:33   # 3
DavidBD
חבר על
 
מיני פרופיל
תאריך הצטרפות: Dec 2005
גיל: 30
הודעות: 956

DavidBD לא מחובר  

ישמצב שמישהו מקמפל ומעלה או שתתנו קומפיילר?
__________________
דוד בן דוד.
  Reply With Quote
ישן 04-05-08, 18:42   # 4
4ior
חבר וותיק
 
4ior's Avatar
 
מיני פרופיל
תאריך הצטרפות: Oct 2005
הודעות: 1,451

4ior לא מחובר  

מסקירה מהירה על הקוד זה מזכיר לי גרסה שראיתי איפה שהוא..השתמשת במדריך?
  Reply With Quote
השב

חברים פעילים הצופים באשכול זה: 1 (0 חברים ו- 1 אורחים)
 


חוקי פירסום
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is מופעל
סמיילים הם מופעל
[IMG] קוד מופעל
קוד HTML מכובה

קפיצה לפורום


כל הזמנים הם GMT +2. הזמן כעת הוא 09:35.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ