public static bool AddStatistics(String name, Player winner, int turns) { string strConnection = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=Battleship.accdb;"; string strSQL = ""; OleDbConnection myConnection = new OleDbConnection(strConnection); OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection); try { myConnection.Open(); strSQL = "INSERT INTO Statistic (Name, Turns, IsWinner) " + "VALUES ('" + name + "'," + turns + "," + (winner == Player.Human) + ")"; myCommand.CommandText = strSQL; myCommand.CommandType = System.Data.CommandType.Text; myCommand.ExecuteNonQuery(); myConnection.Close(); return true; } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } myConnection.Close(); return false; }
/// <summary> /// Starts a new game. /// </summary> /// <remarks> /// Creates an AI player based upon the _aiSetting. /// </remarks> public static void StartGame() { if (_theGame != null) EndGame(); //Create the game _theGame = new BattleShipsGame(); //create the players switch (_aiSetting) { case AIOption.Easy: _ai = new AIEasyPlayer (_theGame); break; case AIOption.Medium: _ai = new AIMediumPlayer(_theGame); break; case AIOption.Hard: _ai = new AIHardPlayer(_theGame); break; default: _ai = new AIHardPlayer(_theGame); break; } _human = new Player(_theGame); //AddHandler _human.PlayerGrid.Changed, AddressOf GridChanged _ai.PlayerGrid.Changed += GridChanged; _theGame.AttackCompleted += AttackCompleted; AddNewState(GameState.Deploying); }
//fire to the proper board public bool Fire(int row, int column) { if (!IsOver) { if (player == Player.Computer) { if (boards[0].Fire(row, column)) { player = Player.Human; return true; } } else if (player == Player.Human) { if (boards[1].Fire(row, column)) { player = Player.Computer; turns++; return true; } } } return false; }
public void NewGame() { player = Player.Human; boards[0].NewGame(); boards[1].NewGame(); turns = 0; }