Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added random factor on server side to the angle #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/domain/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ export default class Bot {
}

handleMove(angle: number, map: Map): void {
this.angle = angle
this.angle = this.pummiFactor(angle)
console.log(this.position.x)
this.position.updatePosition(angle, map.getSpeedAtPosition(this.position))
this.position.updatePosition(
this.angle,
map.getSpeedAtPosition(this.position),
)
Log.moveMessage(this.name, this.position)
}

pummiFactor(angle: number): number {
// Standard normal distribution with box-muller transform https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
let u1 = Math.random()
let u2 = Math.random()
let z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2)
return angle + z0 * (Math.PI / 3)
}

handleStamp(map: Map): void {
const { x, y } = this.position
const closeEnoughToCheckpoint = map.closeEnoughToCheckpoint(this.position)
Expand Down
2 changes: 1 addition & 1 deletion src/domain/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class Map {
}

getSpeedAtPosition(position: Position): number {
return 1
return 10
}

closeEnoughToCheckpoint(position: Position): boolean {
Expand Down
20 changes: 8 additions & 12 deletions src/example/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,11 @@ const joinMessage = {
}

const createConnection = (): void => {
const socket = connect(
PORT,
'localhost',
() => {
// Send the initial message once connected
console.log(`Joining: ${JSON.stringify(joinMessage)}`)
socket.write(JSON.stringify(joinMessage))
},
)
const socket = connect(PORT, 'localhost', () => {
// Send the initial message once connected
console.log(`Joining: ${JSON.stringify(joinMessage)}`)
socket.write(JSON.stringify(joinMessage))
})

const handleMapMessage = (socket: Socket, message: MapMessage): void => {
Log.info(`Received map: ${JSON.stringify(message.data)}`)
Expand Down Expand Up @@ -150,10 +146,10 @@ const createConnection = (): void => {
return
}
if (
// Have a discussion with Jarno - or someone who knows goddamn math
// Could also use norm in math.js: https://mathjs.org/docs/reference/functions/norm.html
Math.sqrt(
(currentPosition.x - target.x) * (currentPosition.x - target.x) +
(currentPosition.y - target.y) * (currentPosition.y - target.y),
(currentPosition.x - target.x) ** 2 +
(currentPosition.y - target.y) ** 2,
) < 5.0
) {
if (target === map.goal) {
Expand Down