Skip to content

Commit

Permalink
Modify random number generator
Browse files Browse the repository at this point in the history
  • Loading branch information
LordierClaw committed Nov 24, 2022
1 parent d247216 commit 6b6d9e5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
8 changes: 5 additions & 3 deletions AmericanChess/AmericanChess.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{993a9192-3a17-456e-9d80-8d4368a0f32b}</ProjectGuid>
<RootNamespace>AmericanChess</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand All @@ -48,9 +48,10 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>NotSet</CharacterSet>
<UseOfMfc>Dynamic</UseOfMfc>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -129,6 +130,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>sfml-graphics.lib;sfml-window.lib;sfml-system.lib;sfml-network.lib;sfml-audio.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions AmericanChess/source/GameObject/ChessObject/ChessPiece.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void ChessPiece::init(ChessPosition pos) {
if (m_type == PLAYER) return;
m_health = GameRule->getHealthChess(m_type);
m_queueSize = GameRule->getQueueSizeChess(m_type);
m_turnLeft = reinterpret_cast<long long>(this) % m_queueSize + 1;
m_turnLeft = GameMath::getRandom(1, m_queueSize);
}

void ChessPiece::init(ChessPosition pos, int health, int turnLeft, int queueSize) {
Expand All @@ -62,7 +62,7 @@ void ChessPiece::update(float deltaTime) {
case IDLE:
if (m_isPromotion) {
std::vector<std::string> promoteList = { "W_Queen", "W_Knight", "W_Rook", "W_Bishop" };
promote(promoteList[reinterpret_cast<long long>(this) % promoteList.size()]);
promote(promoteList[GameMath::getRandom(0, promoteList.size()-1)]);
}
break;
case READY_TO_MOVE:
Expand Down
6 changes: 1 addition & 5 deletions AmericanChess/source/GameObject/ChessObject/Shotgun.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Shotgun.h"
#include "../GameRuleManager.h"
#include <cstdlib>
#include <ctime>

Shotgun::Shotgun() {
m_isShooting = false;
Expand Down Expand Up @@ -82,14 +80,12 @@ void Shotgun::render() {
}

void Shotgun::shoot() {
srand(time(0));
sf::Vector2f pos = this->getPosition();

for (int i = 0; i < GameRule->getShotgunSpray(); i++) {
Bullet* bullet = new Bullet();
float angle = this->getRotation();
int range = SHOOTING_RANGE_ANGLE*7/9;
angle = (angle + rand() % range) - range / 2;
angle = angle + GameMath::getRandom(-range / 2, range / 2);
angle = GameMath::degreeToRad(angle);
pos.x += 8.f * cos(angle);
pos.y += 8.f * sin(angle);
Expand Down
7 changes: 7 additions & 0 deletions AmericanChess/source/Misc/GameMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ float GameMath::radToDegree(float angle) {
float GameMath::degreeToRad(float angle) {
return angle * PI / 180;
}

int GameMath::getRandom(int start, int end) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(start, end);
return dist(gen);
}
4 changes: 4 additions & 0 deletions AmericanChess/source/Misc/GameMath.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <cmath>
#include <random>
#include "SFML/Graphics.hpp"

#define PI 3.141592f
Expand All @@ -15,4 +16,7 @@ class GameMath {
static float radToDegree(float angle);
//convert degree to radian
static float degreeToRad(float angle);

//get Random number
static int getRandom(int start, int end);
};

0 comments on commit 6b6d9e5

Please sign in to comment.