Nine men morris游戏C ++ [英] Nine mens morris game C++

查看:65
本文介绍了Nine men morris游戏C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个练习,我需要练习Nine Mens Morris游戏

我需要帮助在游戏中找到有效序列的算法



目前我已经实施了游戏板,将棋子插入棋盘并找到了邻居(虽然我也很乐意提出优化建议。)



创建序列的目标是,如果玩家X的长度和宽度(不是对角线)有三个序列,那么X可以选择一个他可以从中移除的Y玩家游戏

它没有完成,有点乱我知道它



这就是我安排好的桌子

https://i.stack.imgur.com/srXgI.jpg [ ^ ]


每个
磨坊我附近他的邻居

问题是:如何即时通讯请求sequnce搜索?



我遇到问题的功能是rules.Cpp:

I have an exercise where I need to exercise the Nine Mens Morris game
And I need help with the algorithm of finding valid sequences in the game

For the time being i've implemented the board of the game, inserting pieces to the board and finding the neighbors (although in an elegant way that too I would have been happy to propose optimization.)

The goal in creating sequences is that if player X has a sequence of three pieces in length and width (not diagonally) then X can select a piece of Y player that he can remove from the game
it's not finished and kind of messy i know it

this is how i arranged the table
https://i.stack.imgur.com/srXgI.jpg[^]

for each mill i attached his neighbours
The question is:How to implement the sequnce search?

the function that I have problem with is in "rules.Cpp" :

bool check_if_trio(int row,int col, Board& b,char player);





我尝试过:



board.h



What I have tried:

board.h

#ifndef BOARD_H
#define BOARD_H

#include<iostream>
#include<ctype.h>

#include "Rules.h"

using namespace std;
class Rules;

class Board
{
public:
  Board ();
  Board (const Board& orig);

  void print();
  virtual ~Board ();

    /***Getters & Setters*******/
  char get_pos_char(int row, int col) const;
  void set_pos(int row, int col,char player) ;
    static const int getCols ();
    void set_neighbour();
    int ** get_neighbours (int index_of_MIll);
    int * get_miil(int row,int col,int * index=NULL);



  static const int getRows ()
  {
    return rows;
  }
private:



  static const int cols=13;
  static const int rows=7;
  char board[rows][cols];
  int *neighbours[24][4];
  int mills[24][2];
  int  junc[3][7][2];
  enum {MAX_MIILS=24};
  enum{up=0,right=1,down=2,left=3};
};

#endif /* BOARD_H */



board.cpp

#include "Board.h"

Board::Board ()
{
  int i,j,k=0;
  for(i=0;i<rows;i++)
    for(j=0;j<cols;j++)
      {
	 // spaces
	if ((j%2 == 1) &&( j !=0) )
	   board[i][j] = ' ';
	//to make sure that the next if wouldn't put o in the center
	else 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 board
	else
	  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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆