Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // V0.05
- #include <iostream>
- #include <windows.h>
- #include <ctime> // עבור מספר רנדומלי
- using namespace std;
- //מדריך צבעים https://www.youtube.com/watch?v=IXtm7cI21vM
- void setcolor(unsigned short color){
- HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hcon, color);
- }
- enum status{empty, white, black};
- enum status main_board [7][6]; /// i,j // הוא 0 j השורה הכי עליונה היא השורה בה
- void reset_board(enum status board [7][6]){
- for(int i=0; i<7; i++){
- for(int j=0; j<6; j++){
- board[i][j] = empty;
- }
- }
- }
- void show(enum status board [7][6]){
- cout<<"\n\n1234567\n_______\n";
- for(int j=0; j<6; j++){
- for(int i=0; i<7; i++){
- if(board[i][j]== empty) cout<<"@";
- else if (board[i][j]== white) {
- setcolor(11);
- cout<<"w";
- setcolor(7);
- }
- else {
- setcolor(12);
- cout<<"b";
- setcolor(7);
- }
- }
- cout<<"\n";
- }
- cout<<"\n";
- }
- int get_from_player(enum status stat){
- show(main_board);
- if (stat==white) cout<<"-White move.\n";
- else cout<<"-Black move.\n";
- int location;
- cout <<"Enter location: ";
- cin>> location;
- while(location<1 || location>7 || main_board[location-1][0] != empty){
- cout<<"Please enter a legal location: ";
- cin >> location;
- }
- cout<<"\n";
- return location-1;
- }
- void Put_in_a_move(enum status board [7][6], int location, enum status stat){
- for(int j=5; j>=0; j--){
- if (board[location][j] == empty){
- board[location][j] = stat;
- break;
- }
- }
- }
- enum status Victory_check (enum status board [7][6]){
- for(int j=5; j>=0; j--){
- for(int i=0; i<4; i++){
- if (board[i][j]==empty) continue;
- for(int b=i+1, q=0; q<3; q++, b++){
- if (board[i][j]!=board[b][j]) break;
- if(q==2) return board[i][j];
- }
- }
- }
- // עד כאן בדיקת ניצחונות אופקיים
- for(int i=0; i<7; i++){
- for(int j=5; j>2; j--){
- if (board[i][j]==empty) continue;
- for(int b=j-1, q=0; q<3; q++, b--){
- if (board[i][j]!=board[i][b]) break;
- if(q==2) return board[i][j];
- }
- }
- }
- // עד כאן בדיקת ניצחונות ישרים
- for(int j=5; j>2; j--){
- for(int i=0; i<4; i++){
- if (board[i][j]==empty) continue;
- for(int q=1; q<4; q++){
- if (board[i][j]!=board[i+q][j-q]) break;
- if(q==3) return board[i][j];
- }
- }
- }
- // עד כאן אלכסון משמאל נמוך לימין גבוה
- for(int j=5; j>2; j--){
- for(int i=3; i<7; i++){
- if (board[i][j]==empty) continue;
- for(int q=1; q<4; q++){
- if (board[i][j]!=board[i-q][j-q]) break;
- if(q==3) return board[i][j];
- }
- }
- }
- // עד כאן אלכסון מימין נמוך לשמאל גבוה
- return empty;
- }
- /* מכאן פונקציות לאדם נגד מחשב */
- void clear_location(enum status board [7][6], int loc){
- for(int j=0; j<6; j++){
- if (board[loc][j] != empty){
- board[loc][j] = empty;
- break;
- }
- }
- }
- int get_random_location(enum status board [7][6]){
- int x;
- while(1){
- x = rand() % 11;
- if(x==10)x=3;
- else if(x>6) x-=5;
- if(board[x][0]!=empty) continue;
- return x;
- }
- }
- int check_for_winning_location(enum status board [7][6], enum status stat){
- enum status VictoryFlag = empty;
- for(int i=0; i<7; i++){
- if(board[i][0] != empty) continue;
- Put_in_a_move(board, i, stat);
- VictoryFlag = Victory_check(board);
- clear_location(board, i); // מחזירים את הלוח לאיך שהוא היה לפני השינוי
- if(VictoryFlag == stat) return i;
- }
- return -1; // המשמעות היא שאין מיקום שיניב ניצחון לצבע הזה
- }
- int get_from_AI(enum status board [7][6], enum status stat, int Count_of_actions){
- int loc;
- enum status enemy_color = (stat==white)? black : white;
- if (Count_of_actions == 0 || Count_of_actions == 1) return 3; // תור ראשון ושני לשים באמצע
- loc = check_for_winning_location(board, stat); // נצח אם אפשר
- if(loc!=-1) return loc;
- loc = check_for_winning_location(board, enemy_color); // חסום ניצחון מיידי של היריב
- if(loc!=-1) return loc;
- return get_random_location(board);// עד שנכתוב קוד חכם נשאיר את המיקום האקראי
- }
- int get_from_AI_with_show(enum status board [7][6], enum status stat, int Count_of_actions){
- show(main_board);
- if (stat==white) cout<<"-White AI move...\n";
- else cout<<"-Black AI move...\n";
- Sleep(1100);
- int location = get_from_AI(board, stat, Count_of_actions);
- cout <<"location (AI): "<<location<<"\n\n";
- return location;
- }
- void main(){
- srand(time(0)); // עבור מספר רנדומלי
- enum status VictoryFlag = empty;
- char strMODE[10];
- reset_board(main_board);
- int Count_of_actions = 0, loc;
- do{
- cout<<"AI or Friend mode (A/F): ";
- cin>>strMODE;
- }while(strMODE[0] != 'a' && strMODE[0] != 'A' && strMODE[0] != 'f' && strMODE[0] != 'F');
- if(strMODE[0] == 'f' || strMODE[0] == 'F'){ // אם זה אדם נגד אדם
- do{
- loc = get_from_player(white);
- Put_in_a_move(main_board, loc, white);
- VictoryFlag = Victory_check(main_board);
- if (VictoryFlag != empty) break;
- Count_of_actions++;
- if(Count_of_actions==42) break;
- loc = get_from_player(black);
- Put_in_a_move(main_board, loc, black);
- VictoryFlag = Victory_check(main_board);
- if (VictoryFlag != empty) break;
- Count_of_actions++;
- }while(Count_of_actions<42);
- }
- else{ // אם זה אדם נגד מחשב
- do{
- loc = get_from_player(white);
- Put_in_a_move(main_board, loc, white);
- VictoryFlag = Victory_check(main_board);
- if (VictoryFlag != empty) break;
- Count_of_actions++;
- if(Count_of_actions==42) break;
- loc = get_from_AI_with_show(main_board, black, Count_of_actions);
- Put_in_a_move(main_board, loc, black);
- VictoryFlag = Victory_check(main_board);
- if (VictoryFlag != empty) break;
- Count_of_actions++;
- }while(Count_of_actions<42);
- }
- show(main_board);
- if (VictoryFlag==white) {setcolor(11); cout<<"White won!!!!!\n\n\n\n"; setcolor(7);}
- else if (VictoryFlag==black) {setcolor(12); cout<<"Black won!!!!!\n\n\n\n"; setcolor(7);}
- else {setcolor(15); cout<<"End! No win.\n\n\n\n"; setcolor(7);}
- }
Advertisement
Add Comment
Please, Sign In to add comment