File indexing completed on 2024-10-06 03:45:23
0001 /* 0002 This file is part of the KDE games kwin4 program 0003 SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef AIBOARD_H 0009 #define AIBOARD_H 0010 0011 // own 0012 #include "kwin4global.h" 0013 // Qt 0014 #include <QDataStream> 0015 0016 /** 0017 * Store the values of the cached positions for s set of levels. 0018 */ 0019 class AIValue 0020 { 0021 /** 0022 * Support streaming. 0023 * @param s The stream 0024 * @param v The value to stream 0025 * @return The stream. 0026 */ 0027 friend QDataStream &operator<<(QDataStream &s, const AIValue &v); 0028 0029 /** 0030 * Support streaming. 0031 * @param s The stream 0032 * @param v The value to stream 0033 * @return The stream. 0034 */ 0035 friend QDataStream &operator>>(QDataStream &s, AIValue &v); 0036 0037 public: 0038 /** 0039 * Constructor. 0040 */ 0041 AIValue(); 0042 0043 /** 0044 * Set a value for the given level. 0045 * @param level The level 0046 * @param value The value 0047 */ 0048 void setValue(int level, long value); 0049 0050 /** 0051 * Get the value for a given level. 0052 * @param level The level 0053 * @return The value. 0054 */ 0055 long value(int level) const; 0056 0057 /** 0058 * Checks whether a value is set for the given level. 0059 * @param level The level 0060 * @return True if it is set. 0061 */ 0062 bool isSet(int level) const; 0063 0064 /** 0065 * How many levels do we support. 0066 */ 0067 const static int NO_OF_LEVELS = 5; 0068 0069 private: 0070 // Store the level values 0071 long mValue[NO_OF_LEVELS]; 0072 }; 0073 0074 /** 0075 * Support streaming. 0076 * @param s The stream 0077 * @param v The value to stream 0078 * @return The stream. 0079 */ 0080 QDataStream &operator<<(QDataStream &s, const AIValue &value); 0081 0082 /** 0083 * Support streaming. 0084 * @param s The stream 0085 * @param v The value to stream 0086 * @return The stream. 0087 */ 0088 QDataStream &operator>>(QDataStream &s, AIValue &value); 0089 0090 /** 0091 * Store an AI field in a compressed structure. One row of pieces 0092 * is stored as bit sequence with two bits for each field. The bits 0093 * correspond to the colors (Nobody, Yellow, Red, unused). 0094 */ 0095 class AIBoard 0096 { 0097 /** 0098 * Support streaming. 0099 * @param s The stream 0100 * @param v The value to stream 0101 * @return The stream. 0102 */ 0103 friend QDataStream &operator<<(QDataStream &, const AIBoard &); 0104 0105 /** 0106 * Support streaming. 0107 * @param s The stream 0108 * @param v The value to stream 0109 * @return The stream. 0110 */ 0111 friend QDataStream &operator>>(QDataStream &, AIBoard &); 0112 0113 /** 0114 * Qt Hast function. 0115 * @param board The board to hash. 0116 * @return The hash values. 0117 */ 0118 friend uint qHash(const AIBoard &board); 0119 0120 public: 0121 /** 0122 * Construct an object 0123 */ 0124 AIBoard(); 0125 0126 /** 0127 * Construct board from AI field structure 0128 * @param current The current mover's color 0129 * @param field The game field 0130 */ 0131 AIBoard(uchar current, const char field[][7]); 0132 0133 /** 0134 * Compare to boards. 0135 * @param other The other board 0136 * @return True if the boards are equal. 0137 */ 0138 bool operator==(const AIBoard &other) const; 0139 0140 /** 0141 * Retrieve a mirrored board. The original is not 0142 * changed. 0143 * @param The mirrored board. 0144 */ 0145 AIBoard mirror() const; 0146 0147 /** 0148 * Debug function to print out a board to the console. 0149 */ 0150 void print() const; 0151 0152 /** 0153 * Convert an AI field to the board structure. 0154 * @param current The current mover's color 0155 * @param mirror Mirror the board? 0156 * @param field The game field 0157 */ 0158 void fromField(uchar current, bool mirror, const char field[][7]); 0159 0160 private: 0161 // Store board pieces per row 0162 ushort mBoard[6]; 0163 // Mover's color 0164 uchar mCurrent; 0165 }; 0166 0167 /** 0168 * Support streaming. 0169 * @param s The stream 0170 * @param v The value to stream 0171 * @return The stream. 0172 */ 0173 QDataStream &operator<<(QDataStream &s, const AIBoard &board); 0174 0175 /** 0176 * Support streaming. 0177 * @param s The stream 0178 * @param v The value to stream 0179 * @return The stream. 0180 */ 0181 QDataStream &operator>>(QDataStream &s, AIBoard &board); 0182 0183 /** 0184 * Qt Hast function. 0185 * @param board The board to hash. 0186 * @return The hash values. 0187 */ 0188 uint qHash(const AIBoard &key); 0189 0190 #endif