[Gamesnws-commits] r25 - pkg/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Aug 12 13:00:30 CEST 2009
Author: grafab
Date: 2009-08-12 13:00:23 +0200 (Wed, 12 Aug 2009)
New Revision: 25
Modified:
pkg/R/adminUno.R
pkg/R/playUno.R
Log:
winners points are shown at the end
handcards are shown before asked for penalty-concatenation
Test if workspace allready exists on server
logfile is written when startUnoGame(*,log!=0,*)
Modified: pkg/R/adminUno.R
===================================================================
--- pkg/R/adminUno.R 2009-08-11 13:50:11 UTC (rev 24)
+++ pkg/R/adminUno.R 2009-08-12 11:00:23 UTC (rev 25)
@@ -4,18 +4,27 @@
createUnoGame <- function(wsName, ...)
{
require(nws)
-
+ nwss<-nwsServer(...)
+ serverlist<-nwsListWss(nwss, showDataFrame=TRUE)
+ servernamebool<-TRUE
+ for(i in 1:length(serverlist$Name)){
+ if(wsName==serverlist$Name[[i]]){
+ servernamebool<-FALSE
+ }
+ }
+ if(servernamebool){
# create nws
ws <- netWorkSpace(wsName, ...)
# declare variables in nws
- nwsDeclare(ws, 'players', 'fifo') # list of players for player rotation
+ #ToDo usernames not valid: players, played...
+ nwsDeclare(ws, 'players', 'fifo') # list of players for player rotation
nwsDeclare(ws, 'played', 'lifo') # played card
nwsDeclare(ws, 'cards', 'fifo') # stack of cards
nwsDeclare(ws, 'players_logedin', 'single') # vector of loged-in players
nwsDeclare(ws, 'player_in_action', 'single') # player in action
nwsDeclare(ws, 'winner', 'single') # name of winner
- nwsDeclare(ws, 'penalty', 'single') #if one player got penalty but can not play a card, next player should not get penalty too
+ nwsDeclare(ws, 'penalty', 'single') # value of penalty
nwsDeclare(ws, 'debug' , 'single') # boolean for debug-mode
nwsDeclare(ws, 'points', 'single') # vector of points, in order of players_logedin
nwsDeclare(ws, 'uno' , 'single') #vector of uno-booleans, in order of players_logedin
@@ -56,6 +65,10 @@
cat("start the game with the command 'startUnoGame(ws)'\n\n")
return(ws)
+ }
+ else{ #if workspacename allready exists
+ cat("Workspacename allready exists, please try another name\n")
+ }
}
##########################################################
@@ -63,12 +76,13 @@
# especially to get commands from master-user
# and to wait for other players
############################################################
-startUnoGame <- function(ws, cardsStart=7,
+startUnoGame <- function(ws, cardsStart=3,
minPlayers=2, maxPlayers=10,
- log=FALSE, logfile=NULL, debug=FALSE)
+ log=0, logfile=NULL, debug=FALSE)
{
require(nws)
-
+ wsclass<-class(ws)
+ if(wsclass[1]=="netWorkSpace"){
readCommand <- ""
while(readCommand != "e"){
@@ -94,9 +108,9 @@
.playUnoMaster(ws, players, cardsStart, log=log, logfile=logfile, debug=TRUE)
cat("For replay you have to reset the Game: createUnoGame()\n")
readCommand <- readline("End Game [e]?")
- } else if( !(nplayers>=minPlayers) && readCommand=="s"){
+ } else if( !(nplayers>=minPlayers) && (readCommand=="s" || readCommand=="d")){
cat("You need more than ", nplayers," player!\n")
- } else if( !(nplayers<=maxPlayers) && readCommand=="s"){
+ } else if( !(nplayers<=maxPlayers) && (readCommand=="s" || readCommand=="d")){
cat("You can only play with less than ", nplayers," players!\n")
} else if(readCommand=="o")
cat("Players:", players, "\n")
@@ -106,6 +120,9 @@
nwsDeleteWs(ws at server, ws at wsName)
nwsClose(ws)
cat("GAME OVER \n")
+ }else{
+ cat("ws is no valid networkspace, please create one")
+ }
}
@@ -116,7 +133,7 @@
# * monitor game
##############################################################
.playUnoMaster <- function(ws, players,
- cardsStart, log=FALSE, logfile=NULL, debug)
+ cardsStart, log=0, logfile=NULL, debug)
{
require(nws)
@@ -188,7 +205,7 @@
#Operation during running game
cat("\tGame is running:\n")
- if(log==TRUE){
+ if(log!=0){
winner <- watchUnoGame(ws, logfile=logfile)
} else {
winner <- .txtProgressBarNWS(ws, 'winner')
@@ -201,11 +218,17 @@
###########################################
# Function to log the game
+# Log default = 0 -> no log
+# 1 -> names, winner , points
+# 2 -> 1, played cards
+# 3 -> 2, handcards
+# 4 -> 3, date, time...
##########################################
watchUnoGame <- function(ws, ..., logfile=NULL)
{
require(nws)
+
#TODO: write to logfile
#TODO: log hand cards of players for statistics
@@ -218,7 +241,8 @@
# read played cards
card_played <- card_played_tmp <- ""
- user <- user_tmp <- ""
+ user <- user_tmp <- "startcard"
+ output<-character()
while( is.null(winner <- nwsFindTry(ws, 'winner'))
|| card_played != card_played_tmp
|| user != user_tmp
@@ -228,12 +252,14 @@
#if played card has changed: output
if( card_played != card_played_tmp ){
cat(user, ": ", card_played, " (",length(nwsFindTry(ws,user)),")\n", sep="")
- user <- nwsFindTry(ws, 'player_in_action')
+ output<-paste(output,user, ";", card_played, ";",length(nwsFindTry(ws,user)),"\n", sep="")
+ user <- nwsFindTry(ws, 'player_in_action')
user_tmp <- user # save player
card_played_tmp <- card_played # save card
# if user has changed and no card: output
}else if( user != user_tmp){
cat(user_tmp, ": NO (",length(nwsFindTry(ws,user_tmp)),")\n", sep="")
+ output<-paste(output,user_tmp, ";NO;",length(nwsFindTry(ws,user_tmp)),"\n", sep="")
#user <- nwsFindTry(ws, 'players')
user_tmp <- user
}
@@ -241,7 +267,25 @@
#code runs to fast for nws
Sys.sleep(0.15)
}
-
+ # to get the last card into logfile
+ #if played card has changed: output
+ if( card_played != card_played_tmp ){
+ cat(user, ": ", card_played, " (",length(nwsFindTry(ws,user)),")\n", sep="")
+ output<-paste(output,user, ";", card_played, ";",length(nwsFindTry(ws,user)),"\n", sep="")
+ user <- nwsFindTry(ws, 'player_in_action')
+ user_tmp <- user # save player
+ card_played_tmp <- card_played # save card
+ # if user has changed and no card: output
+ }else if( user != user_tmp){
+ cat(user_tmp, ": NO (",length(nwsFindTry(ws,user_tmp)),")\n", sep="")
+ output<-paste(output,user_tmp, ";NO;",length(nwsFindTry(ws,user_tmp)),"\n", sep="")
+ #user <- nwsFindTry(ws, 'players')
+ user_tmp <- user
+ }
+ user <- nwsFindTry(ws, 'player_in_action')
+ #code runs to fast for nws
+ Sys.sleep(0.15)
+ write(output,file = 'logfile.txt')
#return winner
return(winner)
}
Modified: pkg/R/playUno.R
===================================================================
--- pkg/R/playUno.R 2009-08-11 13:50:11 UTC (rev 24)
+++ pkg/R/playUno.R 2009-08-12 11:00:23 UTC (rev 25)
@@ -67,7 +67,7 @@
cat("Players: ")
for(p in players){
- cat(">>",p, " - (",length(nwsFindTry(ws,p))," card(s)<< ", sep="")
+ cat(">>",p, " - (",length(nwsFindTry(ws,p))," card(s))<< ", sep="")
}
cat("\n")
@@ -108,13 +108,15 @@
played_number <- strsplit(unlist(played), "-")[[1]][2]
cat("Table:", played,"\n")
+ cards_hand <- nwsFindTry(ws, user)
+ cat("Hand:", sort(unlist(cards_hand)), "\n") #sorted output
+
# PENALTY
- # TODO change for reaction to penalty cards
if( played_number =='2+'&& nwsFind(ws, 'penalty') != 0){
rulesbools<-nwsFindTry(ws, 'rulesbools')
if(rulesbools[3]){
readPC<-""
- while(readPC != "y" && readPC != "n"){
+ while(readPC != "y" && readPC != "n"){
readPC <- readline("Do you want to concatenate penalty?[y/n]")
if(readPC=="y"){
pc<-TRUE
@@ -128,6 +130,7 @@
pen<-nwsFetchTry(ws, 'penalty')
.getpenalty(ws,pen,user,0)
nwsStore(ws, 'penalty', 0)
+ cards_hand <- nwsFindTry(ws, user)
}
}
@@ -149,11 +152,11 @@
pen<-nwsFetchTry(ws, 'penalty')
.getpenalty(ws,pen,user,0)
nwsStore(ws, 'penalty', 0)
+ cards_hand <- nwsFindTry(ws, user)
}
}
- cards_hand <- nwsFindTry(ws, user)
- cat("Hand:", sort(unlist(cards_hand)), "\n") #sorted output
+
# PLAY CARD
tmp <- .playUnoCard(cards_hand, played,
computerPlayer=computerPlayer, computerPlayerFunction=computerPlayerFunction)
@@ -169,10 +172,13 @@
}
# End of game, small output
+ .calculate(ws)
+ points<-nwsFindTry(ws,'points')
+ sumpoints<-sum(points)
if( nwsFindTry(ws,'winner') == user ){
- cat("!! CONGRATULATION,", nwsFindTry(ws,'winner'), " you won !!\n")
+ cat("!! CONGRATULATION,", nwsFindTry(ws,'winner'), " you won with",sumpoints,"points!!\n")
} else
- cat("Sorry you lost, winner:", nwsFindTry(ws,'winner'), "\n")
+ cat("Sorry you lost, winner:", nwsFindTry(ws,'winner'), ",with",sumpoints,"points!!\n")
# close nws connection
nwsClose(ws)
@@ -416,7 +422,7 @@
nwsStore(ws,'points',pointsvec)
}
#########################################################
-#Function for giving penalties
+#Function for getting penalties
#########################################################
.getpenalty <- function(ws,number,playerInAction,reasonnumber)
{
@@ -441,6 +447,7 @@
}
cat("You got ",number," penalty card(s)",reason,"!\n")
nwsStore(ws,playerInAction,cards_hand)
+ cat("Hand:", sort(unlist(cards_hand)), "\n") #sorted output
}
########################################################
#Function to remove card from hand
@@ -536,7 +543,7 @@
##########################################################
.getInfo<-function(ws)
{
- require(ws)
+ require(nws)
maxi<- -Inf
mini<- Inf
sumi<- 0
@@ -544,7 +551,6 @@
players<-nwsFindTry(ws,'players_logedin')
for(p in players){
len<-length(nwsFindTry(ws,p))
- cat("len",len,"\n")
if(len > maxi){
maxi <- len
}
@@ -559,7 +565,7 @@
cat("\n ",nwsFindTry(ws,'rulesbools'))
cat("\n Cards in Deck: 102")
cat("\n Players in Game: ",counter)
- cat("\n Max Cards:",maxi)
- cat("\n Min Cards: ",mini)
- cat("\n Average Cards: ",sumi/counter,"\n")
+ cat("\n Max#Cards:",maxi)
+ cat("\n Min#Cards: ",mini)
+ cat("\n Average#Cards: ",sumi/counter,"\n")
}
\ No newline at end of file
More information about the Gamesnws-commits
mailing list