glumis1994 Temat założony przez niniejszego użytkownika |
Algorytm wykrywania pustych miejsc w grze kółko i krzyżyk » 2013-12-14 19:28:33 Witam!!! Chciałbym prosić o pomoc. Napisałem kod na grę kółko i krzyżyk, gracz vs komputer. Komputer losuje miejsce położenia znaku za pomocą funkcji rand. Jednak borykam się cały czas z problemem odrzucenia powtarzających się już miejsc, tzn., że jeśli gracz wybierze pewne miejsce to żeby komputer nie wybrał tego samego. Proszę zwrócić uwagę na algorytm w funkcji "easy", który ma pełnić to zadanie. #include "winbgi2.h" #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h>
void easy( int * tab, int b ); void rysuj( int * tab ); void spr( int * tab );
void main() { int a, b; int tab[ 9 ]; printf( "*******************************************************************************\n" ); printf( "*******************************************************************************\n" ); printf( "***\t\t\t\t\t\t\t\t\t ***\n" ); printf( "***\t\t\t GRA W KOLKO I KRZYZYK \t\t\t ***\n" ); printf( "***\t\t\t\t\t\t\t\t\t ***\n" ); printf( "*** Copyright by Hubert Buczynski ***\n" ); printf( "***\t\t\t\t\t\t\t\t\t ***\n" ); printf( "*******************************************************************************\n" ); printf( "*******************************************************************************\n" ); printf( "\n\n1).........................::Wybierz poziom gry::...............................\n\n \tEASY <1>\t\t\t \tHARD <2>\t:" ); scanf( "%d", & a ); printf( "\n\n2)........................::Wybierz swoj znak, X <1> lub O <0> \t:" ); scanf( "%d", & b ); printf( "\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ZACZYAMY GRE<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n" ); graphics( 900, 900 ); line( 300, 150, 300, 600 ); line( 450, 150, 450, 600 ); line( 150, 300, 600, 300 ); line( 150, 450, 600, 450 ); for( int i = 0; i < 9; i++ ) { tab[ i ] = 12 + i; } if( a == 1 ) easy( tab, b ); wait(); }
void easy( int * tab, int b ) { int c[ 5 ]; int d[ 5 ]; int L = 9; int e = 0; for( int m = 0; m < 5; m++ ) { c[ m ] = 12; d[ m ] = 12; } if( b == 1 ) { for( int i = 0; i < 4; i++ ) { printf( "\nRuch zawodnika: \t" ); scanf( "%d", & c[ i ] ); tab[ c[ i ] - 1 ] = 1; rysuj( tab ); printf( "%d tab[%d]= %d", c[ i ], c[ i ] - 1, tab[ c[ i ] - 1 ] ); spr( tab ); printf( "\nRuch wykonuje komputer\n" ); srand( time( NULL ) ); do { d[ i ] = rand() % L + 1; for( int j = 0; j < 4; j++ ) { if(( d[ i ] != d[ j ] ) &&( d[ i ] != c[ j ] ) ) { tab[ d[ i ] - 1 ] = 0; e = 1; } } } while( e == 0 ); printf( "%d", d[ i ] ); rysuj( tab ); spr( tab ); } printf( "\nRuch zawodnika: \t" ); scanf( "%d", & c[ 5 ] ); tab[ c[ 5 ] - 1 ] = 1; rysuj( tab ); spr( tab ); printf( "%d tab[%d]= %d", c[ 5 ], c[ 5 ] - 1, tab[ c[ 5 ] - 1 ] ); printf( "\n >>> REMIS <<<" ); } else if( b == 0 ) { for( int i = 0; i < 4; i++ ) { printf( "\nRuch zawodnika: \t" ); scanf( "%d", & c[ i ] ); tab[ c[ i ] - 1 ] = 0; rysuj( tab ); printf( "%d tab[%d]= %d", c[ i ], c[ i ] - 1, tab[ c[ i ] - 1 ] ); spr( tab ); printf( "\nRuch wykonuje komputer\n" ); srand( time( NULL ) ); do { d[ i ] =( int )( rand() % L + 1 ); for( int j = 0; j < 4; j++ ) { if(( d[ i ] != d[ j ] ) &&( d[ i ] != c[ j ] ) ) { tab[ d[ i ] - 1 ] = 1; e = 1; } } } while( e == 0 ); printf( "%d", d[ i ] ); rysuj( tab ); spr( tab ); } printf( "\nRuch zawodnika: \t" ); scanf( "%d", & c[ 5 ] ); tab[ c[ 5 ] - 1 ] = 0; rysuj( tab ); spr( tab ); printf( "%d tab[%d]= %d", c[ 5 ], c[ 5 ] - 1, tab[ c[ 5 ] - 1 ] ); printf( "\n >>> REMIS <<<" ); } }
void rysuj( int * tab ) { if( tab[ 0 ] == 1 ) { line( 150, 150, 290, 290 ); line( 290, 150, 150, 290 ); } if( tab[ 0 ] == 0 ) { circle( 225, 225, 70 ); } if( tab[ 1 ] == 1 ) { line( 310, 150, 440, 290 ); line( 310, 290, 440, 150 ); } if( tab[ 1 ] == 0 ) { circle( 375, 225, 70 ); } if( tab[ 2 ] == 1 ) { line( 460, 150, 600, 290 ); line( 460, 290, 600, 150 ); } if( tab[ 2 ] == 0 ) { circle( 525, 225, 70 ); } if( tab[ 3 ] == 1 ) { line( 150, 310, 290, 440 ); line( 150, 440, 290, 310 ); } if( tab[ 3 ] == 0 ) { circle( 225, 375, 70 ); } if( tab[ 4 ] == 1 ) { line( 310, 310, 440, 440 ); line( 310, 440, 440, 310 ); } if( tab[ 4 ] == 0 ) { circle( 375, 375, 70 ); } if( tab[ 5 ] == 1 ) { line( 460, 310, 590, 440 ); line( 460, 440, 590, 310 ); } if( tab[ 5 ] == 0 ) { circle( 525, 375, 70 ); } if( tab[ 6 ] == 1 ) { line( 150, 460, 290, 600 ); line( 150, 600, 290, 460 ); } if( tab[ 6 ] == 0 ) { circle( 225, 525, 70 ); } if( tab[ 7 ] == 1 ) { line( 310, 460, 440, 590 ); line( 310, 590, 440, 460 ); } if( tab[ 7 ] == 0 ) { circle( 375, 525, 70 ); } if( tab[ 8 ] == 1 ) { line( 460, 460, 590, 590 ); line( 460, 590, 590, 460 ); } if( tab[ 8 ] == 0 ) { circle( 525, 525, 70 ); } }
void spr( int * tab ) { if(( tab[ 0 ] == tab[ 1 ] && tab[ 0 ] == tab[ 2 ] && tab[ 2 ] == tab[ 1 ] ) ||( tab[ 3 ] == tab[ 4 ] && tab[ 3 ] == tab[ 5 ] && tab[ 4 ] == tab[ 5 ] ) ||( tab[ 6 ] == tab[ 7 ] && tab[ 6 ] == tab[ 8 ] && tab[ 7 ] == tab[ 8 ] ) ) { printf( " Wygrales, koniec gry" ); wait(); } if(( tab[ 0 ] == tab[ 3 ] && tab[ 3 ] == tab[ 6 ] && tab[ 0 ] == tab[ 6 ] ) ||( tab[ 1 ] == tab[ 4 ] && tab[ 7 ] == tab[ 1 ] && tab[ 4 ] == tab[ 7 ] ) ||( tab[ 2 ] == tab[ 5 ] && tab[ 8 ] == tab[ 5 ] && tab[ 2 ] == tab[ 8 ] ) ) { printf( " Wygrales, koniec gry" ); wait(); } if(( tab[ 0 ] == tab[ 4 ] && tab[ 4 ] == tab[ 8 ] && tab[ 0 ] == tab[ 8 ] ) ||( tab[ 2 ] == tab[ 4 ] && tab[ 4 ] == tab[ 6 ] && tab[ 2 ] == tab[ 6 ] ) ) { printf( " Wygrales, koniec gry" ); wait(); } } |