class != oo

 很多人看到class,就认为是oo,这是一个误区。class并不等于oo,oo强调的是object而不是class,class只是它的实现手段之一。基于这样的误区,有人认为下面的做法是错误的:


  1. class CMyClass 
  2. public
  3.     static void foo(); 
  4.     static void woo(); 
  5.     static void zoo(); 
  6. }; 

理由是CMyClass没有属性没有状态,所以不是oo,这个理由说得过去,但因此认为






本文转自eyjian 51CTO博客,原文链接:http://blog.51cto.com/mooon/431353,如需转载请自行联系原作者



#include <iostream> #include <vector> #include <limits> // 棋盘类 class Board { private: std::vector<std::vector<char>> board; int size; public: Board(int s) : size(s) { board.resize(size, std::vector<char>(size, '*')); } // 显示棋盘 void display() const { for (const auto& row : board) { for (char cell : row) { std::cout << cell << " "; } std::cout << std::endl; } } // 检查位置是否有效 bool isValidMove(int x, int y) const { return x >= 0 && x < size && y >= 0 && y < size && board[x][y] == '*'; } // 落子 void makeMove(int x, int y, char symbol) { board[x][y] = symbol; } // 检查棋盘是否已满 bool isFull() const { for (const auto& row : board) { for (char cell : row) { if (cell == '*') { return false; } } } return true; } // 检查是否获胜 bool checkWin(char symbol) const { // 检查行 for (const auto& row : board) { bool win = true; for (char cell : row) { if (cell != symbol) { win = false; break; } } if (win) return true; } // 检查列 for (int col = 0; col < size; ++col) { bool win = true; for (int row = 0; row < size; ++row) { if (board[row][col] != symbol) { win = false; break; } } if (win) return true; } // 检查主对角线 bool win = true; for (int i = 0; i < size; ++i) { if (board[i][i] != symbol) { win = false; break; } } if (win) return true; // 检查副对角线 win = true; for (int i = 0; i < size; ++i) { if (board[i][size - 1 - i] != symbol) { win = false; break; } } return win; } // 获取棋盘大小 int getSize() const { return size; } // 获取指定位置的棋子 char getCell(int x, int y) const { return board[x][y]; } }; // 玩家类 class Player { private: char symbol; public: Player(char s) : symbol(s) {} // 玩家落子 void makeMove(Board& board) { int x, y; while (true) { std::cout << "请输入你要下棋的位置 (行 列), 范围 0-" << board.getSize() - 1 << ": "; std::cin >> x >> y; if (board.isValidMove(x, y)) { board.makeMove(x, y, symbol); break; } else { std::cout << "无效的位置,请重新输入。" << std::endl; } std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } // 获取玩家棋子 char getSymbol() const { return symbol; } }; // 计算机类 class Computer { private: char symbol; // 计算一行、一列或一条对角线的得分 int calculateScore(const std::vector<char>& line, char symbol) const { std::string lineStr(line.begin(), line.end()); if (lineStr.find("XX") != std::string::npos) { return 50; } else if (lineStr.find("OO") != std::string::npos) { return 25; } else if (lineStr.find("X*") != std::string::npos) { return 10; } else if (lineStr.find("O*") != std::string::npos) { return 8; } else if (lineStr.find("**") != std::string::npos) { return 4; } return 0; } // 评估一个位置的得分 int evaluatePosition(const Board& board, int x, int y) const { int score = 0; // 检查行 score += calculateScore(board.getRow(x), symbol); // 检查列 std::vector<char> col; for (int i = 0; i < board.getSize(); ++i) { col.push_back(board.getCell(i, y)); } score += calculateScore(col, symbol); // 检查主对角线 if (x == y) { std::vector<char> diag1; for (int i = 0; i < board.getSize(); ++i) { diag1.push_back(board.getCell(i, i)); } score += calculateScore(diag1, symbol); } // 检查副对角线 if (x + y == board.getSize() - 1) { std::vector<char> diag2; for (int i = 0; i < board.getSize(); ++i) { diag2.push_back(board.getCell(i, board.getSize() - 1 - i)); } score += calculateScore(diag2, symbol); } return score; } public: Computer(char s) : symbol(s) {} // 计算机落子 void makeMove(Board& board) { int bestScore = -1; int bestX = -1, bestY = -1; for (int x = 0; x < board.getSize(); ++x) { for (int y = 0; y < board.getSize(); ++y) { if (board.isValidMove(x, y)) { int score = evaluatePosition(board, x, y); if (score > bestScore) { bestScore = score; bestX = x; bestY = y; } } } } board.makeMove(bestX, bestY, symbol); } // 获取计算机棋子 char getSymbol() const { return symbol; } }; // 游戏类 class Game { private: Board board; Player player; Computer computer; bool playerFirst; public: Game(int size) : board(size) { char first, symbol; while (true) { std::cout << "你是否要先手? (y/n): "; std::cin >> first; if (first == 'y' || first == 'Y' || first == 'n' || first == 'N') { break; } else { std::cout << "输入无效,请输入 y 或 n。" << std::endl; } std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } while (true) { std::cout << "你要选择的棋子是 (X/O): "; std::cin >> symbol; symbol = std::toupper(symbol); if (symbol == 'X' || symbol == 'O') { break; } else { std::cout << "输入无效,请输入 X 或 O。" << std::endl; } std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } playerFirst = (first == 'y' || first == 'Y'); player = Player(symbol); computer = Computer(symbol == 'X' ? 'O' : 'X'); } // 开始游戏 void play() { while (!board.isFull()) { board.display(); if (playerFirst) { std::cout << "轮到你下棋了。" << std::endl; player.makeMove(board); if (board.checkWin(player.getSymbol())) { board.display(); std::cout << "你赢了!" << std::endl; return; } playerFirst = false; } else { std::cout << "计算机正在下棋..." << std::endl; computer.makeMove(board); if (board.checkWin(computer.getSymbol())) { board.display(); std::cout << "计算机赢了!" << std::endl; return; } playerFirst = true; } } board.display(); std::cout << "平局!" << std::endl; } }; int main() { int size = 3; Game game(size); game.play(); return 0; } 优化代码
09-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值