[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [knavalbattle/multipleShips] src: make it work again
From:       Jaime Torres <jtamate () gmail ! com>
Date:       2014-01-17 16:22:50
Message-ID: E1W4CC2-00070P-5z () scm ! kde ! org
[Download RAW message or body]

Git commit bb6685aab0b8bd4d900805ee658b7ace09b603a9 by Jaime Torres.
Committed on 17/01/2014 at 16:21.
Pushed by jtamate into branch 'multipleShips'.

make it work again

The configuration has to be passed also to the AI to be able to handle
the multiple ships configurations.

M  +5    -4    src/ai/ai.cpp
M  +4    -2    src/ai/ai.h
M  +2    -2    src/ai/dummyai.cpp
M  +1    -1    src/ai/dummyai.h
M  +29   -32   src/ai/smartai.cpp
M  +6    -4    src/ai/smartai.h
M  +4    -4    src/aientity.cpp
M  +19   -2    src/networkentity.cpp

http://commits.kde.org/knavalbattle/bb6685aab0b8bd4d900805ee658b7ace09b603a9

diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp
index 5f19fd1..907ba65 100644
--- a/src/ai/ai.cpp
+++ b/src/ai/ai.cpp
@@ -9,9 +9,10 @@
 
 #include "ai.h"
 
-AI::AI(Sea::Player player, Sea* sea)
+AI::AI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config)
 : m_player(player)
 , m_sea(sea)
+, m_config(config)
 {
 }
 
@@ -27,7 +28,7 @@ Coord AI::desperateMove() const
     return Coord::invalid();
 }
 
-void AI::setShips(const BattleShipsConfiguration* config)
+void AI::setShips()
 {
     // set up computer ships
     // set first the biggest ship, it is more difficult to reach impossible combinations
@@ -35,8 +36,8 @@ void AI::setShips(const BattleShipsConfiguration* config)
     // number of repetitions because the random place is over a previous ship = 0
     bool canFinish = true;
     do {
-        for (int size = config->longestShip(); size >= 1; size--) {
-            for (unsigned int j = 1; j <= config->numberOfShipsOfSize(size); j++) {
+        for (int size = m_config->longestShip(); size >= 1; size--) {
+            for (unsigned int j = 1; j <= m_config->numberOfShipsOfSize(size); j++) {
                 Ship* ship = 0;
                 while (ship == 0 && canFinish) {
                     Coord c(rand() % m_sea->size().x, rand() % m_sea->size().y);
diff --git a/src/ai/ai.h b/src/ai/ai.h
index fa49de4..02c1bfc 100644
--- a/src/ai/ai.h
+++ b/src/ai/ai.h
@@ -11,17 +11,19 @@
 #define AI__AI_H
 
 #include "sea.h"
+#include "ships.h"
 
 class AI
 {
 protected:
     Sea::Player m_player;
     Sea* m_sea;
+    const BattleShipsConfiguration* m_config;
 public:
-    AI(Sea::Player player, Sea* sea);
+    AI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config);
     virtual ~AI() { }
     virtual Coord getMove() = 0;
-    virtual void setShips(const BattleShipsConfiguration* config);
+    virtual void setShips();
     virtual void notify(Sea::Player player, const Coord& c, const HitInfo& hit) = 0;
     Coord desperateMove() const;
 };
diff --git a/src/ai/dummyai.cpp b/src/ai/dummyai.cpp
index 0a4cfa7..bc369f1 100644
--- a/src/ai/dummyai.cpp
+++ b/src/ai/dummyai.cpp
@@ -15,8 +15,8 @@
 #include <time.h>
 #include <sys/time.h>
 
-DummyAI::DummyAI(Sea::Player player, Sea* sea)
-: AI(player, sea)
+DummyAI::DummyAI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config)
+: AI(player, sea, config)
 {
     srand(time(0));
 }
diff --git a/src/ai/dummyai.h b/src/ai/dummyai.h
index 1cbd0a1..884fdcb 100644
--- a/src/ai/dummyai.h
+++ b/src/ai/dummyai.h
@@ -15,7 +15,7 @@
 class DummyAI : public AI
 {
 public:
-    DummyAI(Sea::Player player, Sea* sea);
+    DummyAI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config);
     virtual Coord getMove();
     virtual void notify(Sea::Player, const Coord&, const HitInfo&) { }
 };
diff --git a/src/ai/smartai.cpp b/src/ai/smartai.cpp
index 0e966b3..9d23fc5 100644
--- a/src/ai/smartai.cpp
+++ b/src/ai/smartai.cpp
@@ -29,20 +29,19 @@ public:
     , m_state(state)
     {
     }
-    
+
     virtual ~Strategy() { }
     virtual Coord getMove() = 0;
     virtual Strategy* notify(const Coord& c, const HitInfo& info) = 0;
 };
 
-
 class DestroyStrategy : public Strategy
 {
     Coord m_original;
     Coord m_begin;
     Coord m_end;
     int m_direction;
-    
+
     inline Coord direction() const
     { 
         switch(m_direction) {
@@ -57,14 +56,14 @@ class DestroyStrategy : public Strategy
             return Coord(0, -1);
         }
     }
-    
+
     bool next_try()
     {
         // if it only hit once
         if (m_begin == m_end) {
             // change direction
             m_direction++;
-            
+
             if (m_direction > 3) {
                 return false; // no more to do
             }
@@ -81,12 +80,12 @@ class DestroyStrategy : public Strategy
             else {
                 // change direction on the same line
                 m_direction += 2; 
-                
+
                 // swap begin and end
                 std::swap(m_begin, m_end);
             }
         }
-        
+
         return true;
     }
 public:
@@ -98,7 +97,7 @@ public:
     , m_direction(0)
     {
     }
-    
+
     virtual Coord getMove()
     {
         for (;;) {
@@ -118,7 +117,7 @@ public:
             }
         }
     }
-    
+
     Strategy* notify(const Coord& c, const HitInfo& info)
     {
         if (info.shipDestroyed) {
@@ -149,7 +148,7 @@ public:
     : Strategy(player, sea, state)
     {
     }
-      
+
     virtual Coord getMove()
     {
         for (int i = 0; i < 10000; i++) {
@@ -160,7 +159,7 @@ public:
         }
         return Coord::invalid();
     }
-    
+
     virtual Strategy* notify(const Coord& c, const HitInfo& info)
     {
         if (info.type == HitInfo::HIT &&
@@ -179,7 +178,7 @@ class DiagonalStrategy : public Strategy
     int m_gap;
     int m_offset;
     int m_range;
-    
+
     // following a diagonal, return true if there is water at the enemy's panel.
     bool movesAvailable() const {
         Sea::Player opp = Sea::opponent(m_player);
@@ -192,7 +191,7 @@ class DiagonalStrategy : public Strategy
         }
         return false;
     }
-    
+
     Coord getMoveHelper()
     {
         int index = rand() % m_range;
@@ -224,14 +223,14 @@ class DiagonalStrategy : public Strategy
         
         return Coord::invalid();
     }
-    
+
     void setup()
     {
         do {
             m_offset = rand() % m_gap;
             kDebug() << "offset =" << m_offset << " / " << m_gap;
         } while (!movesAvailable());
-        
+
         m_range = 0;
         for (int y = m_offset; y < m_sea->size().y; y += m_gap) {
             int diag = m_sea->size().y - y;
@@ -258,14 +257,14 @@ public:
 
     virtual Coord getMove()
     {
-    
+
         if (!movesAvailable()) {
             kDebug() << "no moves available";
             setup();
         }
         for (int i = 0; i < 50; i++) {
             Coord c = getMoveHelper();
-            
+
             if (m_sea->canHit(m_player, c)) {
                 return c;
             }
@@ -273,7 +272,7 @@ public:
         
         return Coord::invalid();
     }
-    
+
     virtual Strategy* notify(const Coord& c, const HitInfo& info)
     {
         if (info.type == HitInfo::HIT &&
@@ -287,9 +286,9 @@ public:
     }
 };
 
-SmartAI::SmartAI(Sea::Player player, Sea* sea, bool random)
-: AI(player, sea)
-, m_state(random)
+SmartAI::SmartAI(Sea::Player player, Sea* sea, bool random, const BattleShipsConfiguration* config)
+: AI(player, sea, config)
+, m_state(random, config)
 {
     srand(time(0));
     m_strategy = std::auto_ptr<Strategy>(m_state.defaultStrategy(player, sea));
@@ -323,11 +322,13 @@ void SmartAI::notify(Sea::Player player, const Coord& c, const HitInfo& info)
     }
 }
 
-SmartAI::State::State(bool random)
-: m_random(random)
+SmartAI::State::State(bool random, const BattleShipsConfiguration* config)
+: m_ships()
+, m_random(random)
+, m_config(config)
 {
-    for (int i = 0; i < LARGEST_SHIP; i++) {
-        m_ships[i] = 1;
+    for (unsigned int i = 0; i < m_config->longestShip(); i++) {
+        m_ships[i] = m_config->numberOfShipsOfSize(i+1);
     }
 }
 
@@ -337,12 +338,12 @@ Strategy* SmartAI::State::defaultStrategy(Sea::Player player, Sea* sea)
         return new RandomStrategy(player, sea, *this);
     }
     else {
-        for (int i = LARGEST_SHIP - 1; i >= 0; i--) {
+        for (int i = m_config->longestShip() - 1; i >= 0; i--) {
             if (m_ships[i] > 0 || i == 0) {
                 return new DiagonalStrategy(player, sea, *this, i + 1);
             }
         }
-    
+
         // unreachable
         return 0;
     }
@@ -350,14 +351,10 @@ Strategy* SmartAI::State::defaultStrategy(Sea::Player player, Sea* sea)
 
 void SmartAI::State::destroyed(int size)
 {
-    if (size <= LARGEST_SHIP) {
+    if (size <= m_config->longestShip()) {
         int index = size - 1;
         if (m_ships[index] > 0) {
             m_ships[index]--;
         }
     }
 }
-
-
-
-
diff --git a/src/ai/smartai.h b/src/ai/smartai.h
index 87f4c7d..00f23b2 100644
--- a/src/ai/smartai.h
+++ b/src/ai/smartai.h
@@ -11,6 +11,7 @@
 #define SMARTAI_H
 
 #include <memory>
+#include <QHash>
 
 #include "ai.h"
 #include "sea.h"
@@ -22,10 +23,11 @@ class SmartAI : public AI
 public:
     class State
     {
-        int m_ships[LARGEST_SHIP];
+        QHash<int,int> m_ships;
         bool m_random;
+        const BattleShipsConfiguration* m_config;
     public:
-        explicit State(bool random);
+        explicit State(bool random, const BattleShipsConfiguration* config);
         Strategy* defaultStrategy(Sea::Player player, Sea*);
         void destroyed(int size);
     };
@@ -33,8 +35,8 @@ private:
     std::auto_ptr<Strategy> m_strategy;
     State m_state;
 public:
-    SmartAI(Sea::Player player, Sea* sea, bool random);
-        
+    SmartAI(Sea::Player player, Sea* sea, bool random, const BattleShipsConfiguration* config);
+
     virtual Coord getMove();
     virtual void notify(Sea::Player player, const Coord& c, const HitInfo& hit);
 };
diff --git a/src/aientity.cpp b/src/aientity.cpp
index c0a747c..c829aeb 100644
--- a/src/aientity.cpp
+++ b/src/aientity.cpp
@@ -23,13 +23,13 @@ AIEntity::AIEntity(Sea::Player player, Sea* sea, SeaView *seaview)
 {
     switch (Kg::difficultyLevel()) {
     case KgDifficultyLevel::Easy:
-        m_ai = new DummyAI(m_player, m_sea);
+        m_ai = new DummyAI(m_player, m_sea, sea->battleShipsConfiguration());
         break;
     case KgDifficultyLevel::Medium:
-        m_ai = new SmartAI(m_player, m_sea, true);
+        m_ai = new SmartAI(m_player, m_sea, true, sea->battleShipsConfiguration());
         break;
     default: // hard
-        m_ai = new SmartAI(m_player, m_sea, false);
+        m_ai = new SmartAI(m_player, m_sea, false, sea->battleShipsConfiguration());
         break;
     }
 }
@@ -63,7 +63,7 @@ void AIEntity::start(bool)
 void AIEntity::startPlacing(bool ask)
 {
     m_seaview->setStatus(Sea::PLACING_SHIPS);
-    m_ai->setShips(m_battleShipsConfiguration);
+    m_ai->setShips();
     emit shipsPlaced(m_player);
 }
 
diff --git a/src/networkentity.cpp b/src/networkentity.cpp
index bb64c9d..3cf3017 100644
--- a/src/networkentity.cpp
+++ b/src/networkentity.cpp
@@ -233,14 +233,31 @@ void NetworkEntity::visit(const ChatMessage& msg)
 void NetworkEntity::visit(const GameOptionsMessage& msg)
 {
     bool enabledAdjacentShips = (msg.enabledAdjacentShips() == QString("true"));
-    m_sea->allowAdjacentShips( enabledAdjacentShips );
+    if (m_client) {
+        m_sea->allowAdjacentShips( enabledAdjacentShips );
+    }
 
-    if (enabledAdjacentShips) {
+    if (m_sea->isAdjacentShipsAllowed()) {
         emit chat(i18n("You can place ships adjacent to each other"));
     }
     else {
         emit chat(i18n("You must leave a space between ships"));
     }
+/*
+    bool enabledSeveralShips = (msg.oneOrSeveralShips() == QString("true"));
+    if (m_client) {
+        m_sea->allowSeveralShips( enabledSeveralShips );
+    }
+
+    if (m_sea->isSeveralShipsAllowed()) {
+        emit chat(i18n("You have 4 minesweepers, 3 frigates, 2 cruises and 1 carrier"));
+    }
+    else {
+        emit chat(i18n("You have 1 minesweeper, 1 frigate, 1 cruise and 1 carrier"));
+    }
+*/
+    // Number of ships to sink
+    m_sea->add(m_player, m_sea->isSeveralShipsAllowed() ?  10 : 4);
 }
 
 
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic