//下棋 bool board::step_inside(int x, int y) { if (selected_color != (m_chess_num + 1) % 2) { //cout << "step_inside error: 颜色对不上????" << endl; return 0; } CHESS_COLOR color = static_cast<CHESS_COLOR>((m_chess_num + 1) % 2); /*string c_color; switch (color) { case WHITE:c_color = "后手"; break; case BLACK:c_color = "先手"; break; case NONE:c_color = "没有"; break; default:c_color = "未知"; break; }*/ //who_step++; if (lay(color, x, y)) { m_chess_pos=++m_chess_num; m_chess[m_chess_pos] = data_xytoi(x, y); m_curr_color[x][y] = m_chess_pos; //cout << c_color << "方在(" << x << ',' << y << ")下子" << endl; pre_chess = data_xytoi(x, y); return true; } else { //cout << "错误:在(" << x << ',' << y << ")处不能放下" << c_color << "棋子" << endl; return false; } }
//移棋第二步:在新位置下棋 bool board::move_step(int x, int y) { if (isSelected) { string c_color; switch (selected_color) { case WHITE:c_color = "白"; break; case BLACK:c_color = "黑"; break; case NONE:c_color = "没有颜"; break; default:c_color = "未知"; break; } if (lay(selected_color, x, y)) { int b_x(0), b_y(0); data_itoxy(selected_chess, b_x, b_y); m_chess[m_curr_color[b_x][b_y]] = data_xytoi(x, y); m_curr_color[x][y] = m_curr_color[b_x][b_y]; m_curr_color[b_x][b_y] = 0; cout << c_color << "方把(" << b_x << ',' << b_y << ")移动到(" << x << ',' << y << ')' << endl; isSelected = false; pre_chess = data_xytoi(x, y); return true; } else { int b_x(0), b_y(0); data_itoxy(selected_chess, b_x, b_y); cout << "错误:在(" << b_x << ',' << b_y << ")处不能放下" << c_color << "色棋子,移棋失败" << endl; return false; } } else { cout << "错误:未选中棋子!" << endl; return false; } }
//移棋第二步:在新位置下棋 bool board::move_step(int x, int y) { if (isSelected) { /*string c_color; switch (selected_color) { case WHITE:c_color = "后手"; break; case BLACK:c_color = "先手"; break; case NONE:c_color = "没有"; break; default:c_color = "未知"; break; }*/ if (lay(selected_color, x, y)) { /*int b_x(0), b_y(0); data_itoxy(selected_chess, b_x, b_y);*/ if (m_chess_pos == 0) { //cout << "move_step error: 位置m_chess_pos出错了,为什么??" << endl; } if (m_chess_pos % 2 != selected_color) { //cout << "move_step error :: 放的位置和颜色不一样,为什么??" << endl; } m_chess[m_chess_pos] = data_xytoi(x, y); m_curr_color[x][y] = m_chess_pos; m_chess_pos = 0; //m_curr_color[b_x][b_y] = 0; //cout << c_color << "方把(" << b_x << ',' << b_y << ")移动到(" << x << ',' << y << ')' << endl; isSelected = false; pre_chess = data_xytoi(x, y); // 判断棋面修改 selected_color = (selected_color + 1) % 2; who_step++; m_before[0].push(pre_chess); m_before[1].push( selected_chess); return true; } else { /*int b_x(0), b_y(0); data_itoxy(pre_chess, b_x, b_y);*/ //cout << "错误:在(" << b_x << ',' << b_y << ")处不能放下" << c_color << "色棋子,移棋失败" << endl; return false; } } else { //cout << "错误:未选中棋子!" << endl; return false; } }
//下棋 bool board::step(CHESS_COLOR color, int x, int y) { string c_color; switch (color) { case WHITE:c_color = "白"; break; case BLACK:c_color = "黑"; break; case NONE:c_color = "没有颜"; break; default:c_color = "未知"; break; } if (lay(color, x, y)) { m_chess[++m_chess_num] = data_xytoi(x, y); m_curr_color[x][y] = m_chess_num; cout << c_color << "方在(" << x << ',' << y << ")下子" << endl; pre_chess = data_xytoi(x, y); return true; } else { cout << "错误:在(" << x << ',' << y << ")处不能放下" << c_color << "色棋子" << endl; return false; } }
bool board::step(int x, int y) { if (step_inside(x, y)) { m_before[0].push(data_xytoi(x, y)); m_before[1].push(0); who_step++; selected_color = (selected_color + 1) % 2; return 1; } else return 0; }
void board::init_link() { //初始化棋盘每个棋子的结点 memset(m_network, 0, sizeof(m_network)); // 初始化每个结点对应在棋盘的位置 for (int x(1); x <= board_size; ++x) { for (int y(1); y <= board_size; ++y) { for (int dir(0); dir < dir_count / 2; ++dir) { m_network[x][y][dir].m_index =data_xytoi(x,y)/* x + y * array_size*/; } } } // 初始化棋盘每个位置的4个方向链表的头结点,时间复杂度O(m_size*m_size*4) // direction 表示链表的增加方向 x ,y 表示链表起始位置,也就是空头结点的位置,增加一个空的头结点,方便插入和删除结点 // direction 具体如下 int next_step[m_direction][2] = { {-1,1},{0,1},{1,1},{1,0},{-1,0},{-1,-1},{0,-1},{1,-1}}; int x(0), y(0), direction(0); for (int i(0); i < array_size; ++i) { // init y= i 0<x<=m_n y = i; direction = 3; init_line_head(x, y, direction); // init y-x=i (1<=y<=m_n,1<=x<=m_n) direction = 2; init_line_head(x, y, direction); } y = 0; for (int i(0); i < array_size; ++i) { // init x+y=i; (1<=y<=m_n,1<=x<=m_n) x = i; direction = 0; init_line_head(x, y, direction); // init x=i; (1<=y<=m_n) direction = 1; init_line_head(x, y, direction); // init x-y=i; (1<=y<=m_n,1<=x<=m_n) direction = 2; init_line_head(x, y, direction); } x = board_size + 1; direction = 0; for (int i(0); i < array_size; ++i) { // init x+y=m_n+1+i (1<=y<=m_n,1<=x<=m_n) y = i; init_line_head(x, y, direction); } }
//移棋第一步:选择要移动的棋子并删除它 bool board::move_select(int x, int y) { if (m_curr_color[x][y] == m_inf || m_curr_color[x][y] == 0) { //cout << "move select error :: 这个位置没有棋了????" << endl; return 0; } if (isSelected) { //cout << "move select error :: 上次的棋子还没有放下" << endl; return 0; } if (m_curr_color[x][y] % 2 != selected_color) { //cout << "move select error :: 这个颜色不允许移动" << endl; return 0; } if (m_curr_color[x][y]) { /*string c_color; switch (m_curr_color[x][y]%2) { case WHITE:c_color = "后手"; break; case BLACK:c_color = "先手"; break; case NONE:c_color = "没有"; break; default:c_color = "未知"; break; }*/ //selected_color = m_curr_color[x][y] % 2; m_chess_pos = m_curr_color[x][y]; m_curr_color[x][y] = 0; selected_chess = data_xytoi(x, y); isSelected = true; retract(selected_color, x, y); //cout << "选择在(" << x << ',' << y << ")的" << c_color << "色棋子移动" << endl; return true; } else { //cout << "错误:在(" << x << ',' << y << ")处没有棋子可供选择!" << endl; return false; } }
//移棋第一步:选择要移动的棋子并删除它 bool board::move_select(int x, int y) { if (m_curr_color[x][y]) { string c_color; switch (m_curr_color[x][y]%2) { case WHITE:c_color = "白"; break; case BLACK:c_color = "黑"; break; default:c_color = "未知"; break; } selected_color = static_cast<CHESS_COLOR>(m_curr_color[x][y] % 2); selected_chess = data_xytoi(x, y); isSelected = true; retract(selected_color, x, y); cout << "选择在(" << x << ',' << y << ")的" << c_color << "色棋子移动" << endl; return true; } else { cout << "错误:在(" << x << ',' << y << ")处没有棋子可供选择!" << endl; return false; } }