#include "Board.h"Board::Board (){ int i,j,k=0; for(i=0;i<rows;i++) for(j=0;j<cols;j++) { // spacesif ((j%2 == 1) &&( j !=0) ) board[i][j] = ' ';//to make sure that the next if wouldn't put o in the centerelse if(i==rows/2 && j == cols/2) board[i][j]='-';// (main diagonal)( middle column)(middle row) (secondery diagonal)else if( (i*2==j)||(j == cols/2)||(i == rows/2)|| (i+j==(cols-(i+1))) ) { board[i][j] = 'O'; mills[k][0]=i; mills[k][1]=j; k++; }//rest of the boardelse board[i][j]='-'; } //neighbours =new int** [24]; //for(i=0;i<24;i++) // { // neighbours[i] =new int*[4];// } set_neighbour ();}Board::Board (const Board& orig) { }Board::~ Board () { }void Board::print () { for(int i =0;i<rows;i++) { for(int j=0;j<cols;j++)cout<<board[i][j]; cout<<endl; }}char Board::get_pos_char (int row, int col) const { return board[row][col];}void Board::set_pos (int row, int col, char player) { board[row][col]=player;}void Board::set_neighbour () { l neighbours[0][right]=mills[1]; neighbours[0][down]=mills[9]; neighbours[1][right]=mills[2]; neighbours[1][down]=mills[4]; neighbours[1][left]=mills[0]; neighbours[2][0]=mills[14]; neighbours[2][1]=mills[1]; neighbours[3][0]=mills[4]; neighbours[3][1]=mills[10]; neighbours[4][0]=mills[1]; neighbours[4][1]=mills[5]; neighbours[4][2]=mills[7]; neighbours[4][3]=mills[3]; neighbours[5][0]=mills[13]; neighbours[5][1]=mills[4]; neighbours[6][0]=mills[7]; neighbours[6][1]=mills[11]; neighbours[7][0]=mills[4]; neighbours[7][1]=mills[8]; neighbours[7][2]=mills[6]; neighbours[8][0]=mills[12]; neighbours[8][1]=mills[7]; neighbours[9][0]=mills[0]; neighbours[9][1]=mills[10]; neighbours[9][2]=mills[21]; neighbours[10][0]=mills[3]; neighbours[10][1]=mills[11]; neighbours[10][2]=mills[18]; neighbours[10][3]=mills[9]; neighbours[11][0]=mills[6]; neighbours[11][1]=mills[15]; neighbours[11][2]=mills[10]; neighbours[12][0]=mills[8]; neighbours[12][1]=mills[13]; neighbours[12][2]=mills[17]; neighbours[13][0]=mills[7]; neighbours[13][1]=mills[14]; neighbours[13][2]=mills[20]; neighbours[13][3]=mills[12]; neighbours[14][0]=mills[2]; neighbours[14][1]=mills[23]; neighbours[14][2]=mills[13]; neighbours[15][0]=mills[11]; neighbours[15][1]=mills[16]; neighbours[16][0]=mills[17]; neighbours[16][1]=mills[19]; neighbours[16][2]=mills[15]; neighbours[17][0]=mills[12]; neighbours[17][1]=mills[16]; neighbours[18][0]=mills[10]; neighbours[18][1]=mills[19]; neighbours[19][0]=mills[16]; neighbours[19][1]=mills[20]; neighbours[19][2]=mills[22]; neighbours[19][3]=mills[18]; neighbours[20][0]=mills[13]; neighbours[20][1]=mills[19]; neighbours[21][0]=mills[9]; neighbours[21][1]=mills[22]; neighbours[22][0]=mills[19]; neighbours[22][1]=mills[23]; neighbours[22][2]=mills[21]; neighbours[23][0]=mills[14]; neighbours[23][1]=mills[22]; }int* Board::get_miil (int row, int col,int * index) { for(int i =0;i<MAX_MIILS;i++) if(mills[i][0] == row && mills[i][1] == col) { *index=i; return mills[i]; } return NULL;}int** Board::get_neighbours (int index_of_MIll) { if(index_of_MIll>=0 && index_of_MIll <MAX_MIILS) return neighbours[index_of_MIll]; return NULL;} rules.h rules.h#ifndef RULES_H#define RULES_H#include<iostream>#include<ctype.h>#include <string>#include<stdlib.h>#include "Board.h"using namespace std;class Board;class Rules{public: Rules (); Rules (const Rules& orig); virtual ~Rules (); bool is_vaild_col(char col); // check column validity bool is_vaild_row(int row);// check row validity int to_numeric_col(char col); // Converts an alphabetical value to its equivalent numerical value int correct_row(int row, const Board& b); // convert the number to the correct place uppon the y axis int abs(int num);//Absolute value util function bool answer_parser (const Board& b,string *ans, int* row, int* col,bool* quit) ; void game_over(string player); bool check_if_trio(int row,int col, Board& b,char player); private:};#endif /* RULES_H */ rules.cpp **这里是问题** rules.cpp **here is the problem** <pre>#include "Rules.h"#include "Board.h"Rules::Rules () { }Rules::Rules (const Rules& orig) { }Rules::~ Rules () { }bool Rules::is_vaild_col (char col) { return (toupper(col) >='A' && toupper(col)<= 'G') ; // check if the letter is on the relevant range}bool Rules::is_vaild_row (int row) { return (row>=1 && row<=7); // check if the number is on the relevant range}int Rules::correct_row (int row, const Board& b) { return (abs(row-b.getRows ())); // convert the number to the correct place uppon the y axis}int Rules::to_numeric_col (char col) { return ((toupper (col)-'A')*2); // convert the letter to numeric value and make sure it will not be a space}int Rules::abs (int num) { return num<0 ? -1*num : num; //Absolute value util function}bool Rules::answer_parser (const Board& b,string *ans, int* row, int* col,bool* quit) { if(ans->length () == 4) { if(*ans == "quit" || *ans == "QUIT"){ *quit =true; return true;} } else if(ans->length ()==2) { if(is_vaild_col (ans->at (0)) && is_vaild_row (ans->at (1)-'0'))//(is_vaild_col (ans.at (0)) && is_vaild_row ((atoi(ans.at (1)))){ *row = correct_row (ans->at (1)-'0',b); *col = to_numeric_col (ans->at (0)); return true;} } return false;}void Rules::game_over (string player) { cout<<player<<" wins the game."<<endl;}bool Rules::check_if_trio (int row, int col, Board& b,char player) { int cnt; int index_of_mill; int * current_mill =b.get_miil (row,col,&index_of_mill); int **neighbour =b.get_neighbours (index_of_mill); if(current_mill != NULL) { if(b.get_pos_char (*(neighbour[0]+2),*(neighbour[0]+1))== player) cnt=1; }} main.cpp main.cpp<pre>#include <cstdlib>#include "Board.h"#include "Black.h"#include "White.h"#include "Rules.h"#include <string>using namespace std ;/* * */int main (int argc, char** argv){ int row; int col; string answer; Board b; bool vaild =true; Black black; White white; string player; bool quit = false; Rules r; b.print (); do { do{ do { cout<<"B:"; cin>>answer; if( r.answer_parser (b,&answer,&row,&col,&quit)) { vaild=true; if(black.place_piece (b,col,row) && !quit) {b.print ();r.check_if_trio (row,col,b,'B');break; } else if( ! quit) { cout<<"invaild ;move the game awaits a vaild move."<<endl; vaild=false; } if(quit) {player ="W";break; } } else { cout<<"invaild ;move the game awaits a vaild move."<<endl; //‫‪Invalid‬‬ ‫;‪move‬‬ ‫‪the‬‬ ‫‪game‬‬ ‫‪awaits‬‬ ‫‪a‬‬ ‫‪valid‬‬ ‫‪move.\n‬‬ break; } } while(!vaild); if(quit) break; do { cout<<"W:"; cin>>answer; if( r.answer_parser (b,&answer,&row,&col,&quit)) { vaild=true; if(white.place_piece (b,col,row) && !quit) {b.print ();break; } else if( ! quit) { cout<<"invaild ;move the game awaits a vaild move."<<endl; vaild=false; } if(quit) {player ="B";break; } } else { cout<<"invaild ;move the game awaits a vaild move."<<endl; //‫‪Invalid‬‬ ‫;‪move‬‬ ‫‪the‬‬ ‫‪game‬‬ ‫‪awaits‬‬ ‫‪a‬‬ ‫‪valid‬‬ ‫‪move.\n‬‬ break; } } while(!vaild); if(quit) break; cout<<white.getPieces ()<<endl;} while((white.getPieces ())>0 && !quit); } while(! quit ); r.game_over (player); return 0 ;} black.cpp and white.cpp just have that function black.cpp and white.cpp just have that function<pre>bool Black::place_piece (Board& b,int col, int row) { char pos=b.get_pos_char (row,col); // comfort var to keep the char in the desired position if((pos == '-') ||(pos == 'W') ) // check the validity of the coords//'B' in white return false; b.set_pos (row , col,'B'); // set the right symbol//'W' in white pieces--; return true; }推荐答案You must solve the problems by using the debugger. In the tutorial Learn C++ Chapter 1.11 and follow are the techniques how to use the debugger explained. Watch the memory of your neighbours array to see what is happening. Tip: I am convinced that your set_neighbour() function isnt optimal. I would write some set_mill() functions.You must solve the problems by using the debugger. In the tutorial Learn C++ Chapter 1.11 and follow are the techniques how to use the debugger explained. Watch the memory of your neighbours array to see what is happening.Tip: I am convinced that your set_neighbour() function isnt optimal. I would write some set_mill() functions. 这篇关于Nine men morris游戏C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 06:52