コード例 #1
0
ファイル: PlayerDAT.cs プロジェクト: stevenaw/2PlayerChess
        // Delete the given player
        public bool deletePlayer(Player player)
        {
            bool error = false;
            int currIndex = 0;
            Player currPlayer;
            byte[] source;

            // Check for file existence
            if (File.Exists(this.fullPath))
            {
                source = File.ReadAllBytes(this.fullPath);

                while (!error && currIndex < source.Length)
                {
                    currPlayer = Player.FromBytes(source, ref currIndex);

                    if (currPlayer == null)
                        error = true;
                    else if (currPlayer.ID == player.ID)
                    {
                        byte[] newFile = new byte[source.Length - currPlayer.RecordLength];
                        Array.Copy(source, 0, newFile, 0, currIndex - currPlayer.RecordLength);
                        Array.Copy(source, currIndex, newFile, currIndex - currPlayer.RecordLength, source.Length - currIndex);

                        File.WriteAllBytes(this.fullPath, newFile);
                        break;
                    }
                }
            }

            return !error;
        }
コード例 #2
0
ファイル: PlayerXML.cs プロジェクト: stevenaw/2PlayerChess
        // Delete the given player
        public bool deletePlayer(Player player)
        {
            XmlDocument doc = GetFile(fullPath);
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                Player thisOne = FromElement(node as XmlElement);

                if (player.Equals(thisOne))
                {
                    doc.DocumentElement.RemoveChild(node);
                    doc.Save(fullPath);
                    return true;
                }
            }

            return false;
        }
コード例 #3
0
ファイル: PlayerSQL.cs プロジェクト: stevenaw/2PlayerChess
        // Delete the given player
        public bool deletePlayer(Player player)
        {
            int returnVal = 0;
            const string statement = "DELETE FROM player WHERE id = @id";

            cmd = new SqlCommand(statement, conn);
            cmd.Parameters.AddWithValue("@id", player.ID);

            try
            {
                conn.Open();
                returnVal = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
            }
            finally
            {
                conn.Close();
            }

            return returnVal == 1;
        }
コード例 #4
0
ファイル: PlayerXML.cs プロジェクト: stevenaw/2PlayerChess
        // Update the changeable stats of the player objects passed in
        public bool updatePlayerRatings(Player p1, Player p2)
        {
            XmlDocument doc = GetFile(fullPath);

            foreach (XmlElement elem in doc.DocumentElement.ChildNodes)
            {
                int id = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_ID));
                if (id == p1.ID)
                {
                    elem.SetAttribute(XML_PLAYER_RATING, p1.Rating.ToString());
                    elem.SetAttribute(XML_PLAYER_WINS, p1.Wins.ToString());
                    elem.SetAttribute(XML_PLAYER_LOSSES, p1.Losses.ToString());
                    elem.SetAttribute(XML_PLAYER_DRAWS, p1.Draws.ToString());
                }
                else if (id == p2.ID)
                {
                    elem.SetAttribute(XML_PLAYER_RATING, p2.Rating.ToString());
                    elem.SetAttribute(XML_PLAYER_WINS, p2.Wins.ToString());
                    elem.SetAttribute(XML_PLAYER_LOSSES, p2.Losses.ToString());
                    elem.SetAttribute(XML_PLAYER_DRAWS, p2.Draws.ToString());
                }
            }

            doc.Save(fullPath);

            return true;
        }
コード例 #5
0
ファイル: PlayerXML.cs プロジェクト: stevenaw/2PlayerChess
        // Add the newly-created player in the database.
        public bool saveNewPlayer(Player player)
        {
            XmlDocument doc = GetFile(fullPath);
            doc.DocumentElement.AppendChild(ToElement(player, doc));
            doc.Save(fullPath);

            return true;
        }
コード例 #6
0
ファイル: PlayerXML.cs プロジェクト: stevenaw/2PlayerChess
        private XmlElement ToElement(Player p, XmlDocument root)
        {
            XmlElement node = root.CreateElement(XML_NODE_ENTITY);
            node.SetAttribute(XML_PLAYER_ID, p.ID.ToString());
            node.SetAttribute(XML_PLAYER_NAME, p.Name);
            node.SetAttribute(XML_PLAYER_RATING, p.Rating.ToString());
            node.SetAttribute(XML_PLAYER_WINS, p.Wins.ToString());
            node.SetAttribute(XML_PLAYER_LOSSES, p.Losses.ToString());
            node.SetAttribute(XML_PLAYER_DRAWS, p.Draws.ToString());

            return node;
        }
コード例 #7
0
ファイル: PlayerXML.cs プロジェクト: stevenaw/2PlayerChess
        private Player FromElement(XmlElement elem)
        {
            Player p = new Player();
            p.ID = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_ID));
            p.Name = elem.GetAttribute(XML_PLAYER_NAME);
            p.Rating = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_RATING));
            p.Wins = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_WINS));
            p.Losses = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_LOSSES));
            p.Draws = Convert.ToInt32(elem.GetAttribute(XML_PLAYER_DRAWS));

            return p;
        }
コード例 #8
0
ファイル: PlayerSQL.cs プロジェクト: stevenaw/2PlayerChess
        // Store the newly-created player in the database.
        public bool saveNewPlayer(Player player)
        {
            bool success = true;

            const string insertStatement = "INSERT player (id, playerName, rating, wins, losses, draws) "
                +"VALUES (@id, @playerName, @rating, @wins, @losses, @draws)";

            cmd = new SqlCommand(insertStatement, conn);
            cmd.Parameters.AddWithValue("@id", player.ID);
            cmd.Parameters.AddWithValue("@playerName", player.Name);
            cmd.Parameters.AddWithValue("@rating", player.Rating);
            cmd.Parameters.AddWithValue("@wins", player.Wins);
            cmd.Parameters.AddWithValue("@losses", player.Losses);
            cmd.Parameters.AddWithValue("@draws", player.Draws);

            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                success = false;
            }
            finally
            {
                conn.Close();
            }

            return success;
        }
コード例 #9
0
ファイル: PlayerSQL.cs プロジェクト: stevenaw/2PlayerChess
        // Update the changeable stats of the player object passed in
        public bool updatePlayerRatings(Player p1, Player p2)
        {
            int count = 0;
            const string updateStatement = "UPDATE player SET rating = @rating, wins = @wins, losses = @losses, draws = @draws WHERE id = @id";

            cmd = new SqlCommand(updateStatement, conn);
            cmd.Parameters.AddWithValue("@rating", p1.Rating);
            cmd.Parameters.AddWithValue("@wins", p1.Wins);
            cmd.Parameters.AddWithValue("@losses", p1.Losses);
            cmd.Parameters.AddWithValue("@draws", p1.Draws);
            cmd.Parameters.AddWithValue("@id", p1.ID);

            try
            {
                conn.Open();
                count = cmd.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                count = 0;
            }

            if (count == 1)
            {
                cmd.Parameters.AddWithValue("@rating", p2.Rating);
                cmd.Parameters.AddWithValue("@wins", p2.Wins);
                cmd.Parameters.AddWithValue("@losses", p2.Losses);
                cmd.Parameters.AddWithValue("@draws", p2.Draws);
                cmd.Parameters.AddWithValue("@id", p2.ID);

                try
                {
                    conn.Open();
                    count = cmd.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    count = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            else
                conn.Close();

            return count == 1;
        }
コード例 #10
0
ファイル: Player.cs プロジェクト: stevenaw/2PlayerChess
        /*
         * Unpack an object from a byte array.
         *
         * A Player object is returned on success, though on failure this is null. Supplied
         * parameters must be the source from which to draw, as well as the index from which
         * to reference as the start of the object.
         *
         * The sister function to this is the non-static function ToBytes.
         */
        public static Player FromBytes(byte[] source, ref int offset)
        {
            Player p = null;
            int intSize = sizeof(int);

            if (source.Length - offset > intSize)
            {
                byte[] intField = new byte[intSize];
                Array.Copy(source, offset + intSize, intField, 0, intSize);
                int nameLen = BitConverter.ToInt32(intField, 0);

                // Can hold 5 class-level ints, variable-length name, and length of name as an integer
                if (source.Length - offset >= nameLen + intSize * 6)
                {
                    byte[] nameField = new byte[nameLen];

                    p = new Player("", 0, 0, 0, 0, 0);

                    Array.Copy(source, offset, intField, 0, intSize);
                    p.id = BitConverter.ToInt32(intField, 0);
                    offset += intSize * 2;

                    Array.Copy(source, offset, nameField, 0, nameLen);
                    p.name = Encoding.ASCII.GetString(nameField);
                    offset += nameLen;

                    Array.Copy(source, offset, intField, 0, intSize);
                    p.wins = BitConverter.ToInt32(intField, 0);
                    offset += intSize;

                    Array.Copy(source, offset, intField, 0, intSize);
                    p.losses = BitConverter.ToInt32(intField, 0);
                    offset += intSize;

                    Array.Copy(source, offset, intField, 0, intSize);
                    p.draws = BitConverter.ToInt32(intField, 0);
                    offset += intSize;

                    Array.Copy(source, offset, intField, 0, intSize);
                    p.rating = BitConverter.ToInt32(intField, 0);
                    offset += intSize;
                }
            }

            return p;
        }
コード例 #11
0
ファイル: PlayerDAT.cs プロジェクト: stevenaw/2PlayerChess
        // Add the newly-created player in the database.
        public bool saveNewPlayer(Player player)
        {
            FileStream f;
            byte[] playerAsBytes;

            if (!File.Exists(this.fullPath))
                f = File.Create(this.fullPath);
            else
                f = File.OpenWrite(this.fullPath);

            playerAsBytes = player.ToBytes();
            f.Seek(0, SeekOrigin.End);
            f.Write(playerAsBytes, 0, playerAsBytes.Length);

            return true;
        }
コード例 #12
0
ファイル: PlayerDAT.cs プロジェクト: stevenaw/2PlayerChess
        // Update the changeable stats of the player objects passed in
        public bool updatePlayerRatings(Player p1, Player p2)
        {
            bool error = false;
            int recUpdated = 0;
            int currIndex = 0;
            Player currPlayer;
            byte[] source;
            byte[] playerRec;

            // Check for file existence
            if (File.Exists(this.fullPath))
            {
                source = File.ReadAllBytes(this.fullPath);

                while (!error && recUpdated < 2 && currIndex < source.Length)
                {
                    currPlayer = Player.FromBytes(source, ref currIndex);

                    if (currPlayer == null)
                        error = true;
                    else if (currPlayer.ID == p1.ID)
                    {
                        playerRec = p1.ToBytes();
                        Array.Copy(playerRec, 0, source, currIndex - playerRec.Length, playerRec.Length);
                        recUpdated++;
                    }
                    else if (currPlayer.ID == p2.ID)
                    {
                        playerRec = p2.ToBytes();
                        Array.Copy(playerRec, 0, source, currIndex - playerRec.Length, playerRec.Length);
                        recUpdated++;
                    }
                }

                if (!error && recUpdated == 2)
                    File.WriteAllBytes(this.fullPath, source);
            }

            return recUpdated == 2 && !error;
        }
コード例 #13
0
ファイル: Game.cs プロジェクト: stevenaw/2PlayerChess
        /*
         * Initialize all game-driven variables who's values must be retained for the duration
         * of the game, and are not set with every move (startXCoord, for example).
         */
        public Game(Player p1, Player p2)
        {
            whiteTurn = true;
            kingPos = new BoardSquare[2] { new BoardSquare(4, 0), new BoardSquare(4, 7) };
            gameStatus = GameStatusCodes.normal;
            attackingPieces = new BoardSquare[2] {
                BoardSquare.Empty,
                BoardSquare.Empty,
            };

            allMoves = new List<ChessMove>();
            positions = new BoardPositions(Game.DRAW_LIMIT, BoardPositions.DEFAULT_START);
            PlayerWhite = p1;
            PlayerBlack = p2;

            board = new Board(this);
            board[0, 0] = new Rook(Piece.NOTATION_W);
            board[0, 1] = new Knight(Piece.NOTATION_W);
            board[0, 2] = new Bishop(Piece.NOTATION_W);
            board[0, 3] = new Queen(Piece.NOTATION_W);
            board[0, 4] = new King(Piece.NOTATION_W);
            board[0, 5] = new Bishop(Piece.NOTATION_W);
            board[0, 6] = new Knight(Piece.NOTATION_W);
            board[0, 7] = new Rook(Piece.NOTATION_W);
            board[1, 0] = new Pawn(Piece.NOTATION_W);
            board[1, 1] = new Pawn(Piece.NOTATION_W);
            board[1, 2] = new Pawn(Piece.NOTATION_W);
            board[1, 3] = new Pawn(Piece.NOTATION_W);
            board[1, 4] = new Pawn(Piece.NOTATION_W);
            board[1, 5] = new Pawn(Piece.NOTATION_W);
            board[1, 6] = new Pawn(Piece.NOTATION_W);
            board[1, 7] = new Pawn(Piece.NOTATION_W);

            board[7, 0] = new Rook(Piece.NOTATION_B);
            board[7, 1] = new Knight(Piece.NOTATION_B);
            board[7, 2] = new Bishop(Piece.NOTATION_B);
            board[7, 3] = new Queen(Piece.NOTATION_B);
            board[7, 4] = new King(Piece.NOTATION_B);
            board[7, 5] = new Bishop(Piece.NOTATION_B);
            board[7, 6] = new Knight(Piece.NOTATION_B);
            board[7, 7] = new Rook(Piece.NOTATION_B);
            board[6, 0] = new Pawn(Piece.NOTATION_B);
            board[6, 1] = new Pawn(Piece.NOTATION_B);
            board[6, 2] = new Pawn(Piece.NOTATION_B);
            board[6, 3] = new Pawn(Piece.NOTATION_B);
            board[6, 4] = new Pawn(Piece.NOTATION_B);
            board[6, 5] = new Pawn(Piece.NOTATION_B);
            board[6, 6] = new Pawn(Piece.NOTATION_B);
            board[6, 7] = new Pawn(Piece.NOTATION_B);
        }