From c5605b6693982b8db2b3248d8015c3d6b1066f4d Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 10 Nov 2011 12:27:31 +0100 Subject: [PATCH] Pong and snake now use rand(3). --- games/conway.cpp | 2 +- games/pong.cpp | 35 +++++++++++++++++++++++++++++++---- games/snake.cpp | 20 ++++++-------------- libmaxsi/Makefile | 1 + libmaxsi/c/hsrc/stdlib.h | 2 +- libmaxsi/random.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 libmaxsi/random.cpp diff --git a/games/conway.cpp b/games/conway.cpp index e6965420..98d95d44 100644 --- a/games/conway.cpp +++ b/games/conway.cpp @@ -6,7 +6,7 @@ #include #include #include - + using namespace Maxsi; using namespace Maxsi::Keyboard; diff --git a/games/pong.cpp b/games/pong.cpp index 150d280a..d34ff3ba 100644 --- a/games/pong.cpp +++ b/games/pong.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include using namespace Maxsi; using namespace Maxsi::Keyboard; @@ -107,22 +109,26 @@ void Goal(nat player) { frame->text[ballx + bally*width] = ' ' | (COLOR8_WHITE << 8); + int offset = (rand() % 4) - 2; ballx = width/2; - bally = height/2; + bally = height/2 + offset; if ( player == 1 ) { ballvelx = 1; - ballvely = 1; + ballvely = (rand() % 2) * 2 - 1; p1score++; } else if ( player == 2 ) { ballvelx = -1; - ballvely = -1; + ballvely = (rand() % 2) * 2 - 1; p2score++; } + if ( ballvely > 0 ) { ballvely /= ballvely; } + if ( ballvely < 0 ) { ballvely /= -ballvely; } + System::Sound::SetFrequency(goalfreq); soundleft = 50; @@ -198,8 +204,30 @@ void ReadInput() } } +int usage(int argc, char* argv[]) +{ + printf("usage: %s [OPTIONS]\n", argv[0]); + printf("Options:\n"); + printf(" --speed How many miliseconds between updates\n"); + printf(" --usage Display this screen\n"); + printf(" --help Display this screen\n"); + + return 0; +} + int main(int argc, char* argv[]) { + int sleepms = 50; + for ( int i = 1; i < argc; i++ ) + { + if ( String::Compare(argv[i], "--help") == 0 ) { return usage(argc, argv); } + if ( String::Compare(argv[i], "--usage") == 0 ) { return usage(argc, argv); } + if ( String::Compare(argv[i], "--speed") == 0 && 1 < argc-i ) + { + sleepms = String::ToInt(argv[++i]); + } + } + int result = Init(); if ( result != 0 ) { return result; } @@ -208,7 +236,6 @@ int main(int argc, char* argv[]) ReadInput(); Update(); UpdateUI(); - const unsigned sleepms = 50; Thread::USleep(sleepms * 1000); if ( soundleft < 0 ) { continue; } if ( soundleft <= 50 ) diff --git a/games/snake.cpp b/games/snake.cpp index 71e6bdb0..c1ac17e1 100644 --- a/games/snake.cpp +++ b/games/snake.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace Maxsi; using namespace Maxsi::Keyboard; @@ -36,15 +37,6 @@ const int speedincrease = -5; const int maxspeed = 40; volatile int speed; -// HACK: Sortix has no random number generator yet! -int random_seed=1337; -int rand(int max) -{ - int tmpmax=max; - random_seed = random_seed + 37 * 1103515245 + 12345; - return (unsigned int) (random_seed / 65536) % tmpmax; -} - void Clear() { // Reset the game data. @@ -56,7 +48,7 @@ void Reset() Clear(); tailx = posx = width/2; taily = posy = height/2; - switch ( rand(4) ) + switch ( rand() % 4 ) { case 0: velx = -1; vely = 0; break; case 1: velx = 1; vely = 0; break; @@ -64,8 +56,8 @@ void Reset() case 3: velx = 0; vely = -1; break; } - animalx = 2 + rand(width-4); - animaly = 2 + rand(height-4); + animalx = 2 + (rand() % width-4); + animaly = 2 + (rand() % height-4); taillen = 0; tailmax = 3; @@ -149,8 +141,8 @@ void Update() if ( newx == animalx && newy == animaly ) { tailmax++; - animalx = 2 + rand(width-4); - animaly = 2 + rand(height-4); + animalx = 2 + (rand() % width-4); + animaly = 2 + (rand() % height-4); if ( maxspeed < speed ) { speed += speedincrease; } } diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index 54351a4d..1447ee30 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -55,6 +55,7 @@ thread.o \ io.o \ init.o \ start.o \ +random.o \ HEADERS=\ error.h \ diff --git a/libmaxsi/c/hsrc/stdlib.h b/libmaxsi/c/hsrc/stdlib.h index fd6bf59d..6c32d0f4 100644 --- a/libmaxsi/c/hsrc/stdlib.h +++ b/libmaxsi/c/hsrc/stdlib.h @@ -50,6 +50,7 @@ void exit(int); void _Exit(int status); void free(void*); void* malloc(size_t); +int rand(void); /* TODO: These are not implemented in libmaxsi/sortix yet. */ #ifndef SORTIX_UNIMPLEMENTED @@ -90,7 +91,6 @@ int posix_openpt(int); char* ptsname(int); int putenv(char*); void qsort(void*, size_t, size_t, int (*)(const void*, const void*)); -int rand(void); long random(void); void* realloc(void*, size_t); char* realpath(const char* restrict, char* restrict); diff --git a/libmaxsi/random.cpp b/libmaxsi/random.cpp new file mode 100644 index 00000000..63daa8a4 --- /dev/null +++ b/libmaxsi/random.cpp @@ -0,0 +1,39 @@ +/****************************************************************************** + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + more details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + random.cpp + Provides access to random numbers using various algorithms. + +******************************************************************************/ + +#include "platform.h" + +namespace Maxsi +{ + namespace Random + { + int random_seed=1337; + extern "C" int rand() + { + random_seed = random_seed + 37 * 1103515245 + 12345; + return random_seed / 65536; + } + } +} +