Replacing text within an external file

Hi all,

This is going to sound like a really stupid question, but I have tried numerous ways to find the solution by myself and all I get in return are errors that I dont know anything about.

My problem is as follows -

I have made a simple game of noughts and crosses in applescript that someone can play against a computer. I am trying to put in a leaderboard type function that uses an external text file to store the following data:

  1. A running total of all games played,
  2. A running total of all the games that the player has won
  3. A running total of all games that the computer has won
  4. A log of the actual win conditions that have been played, and the date/ time that they occurred.

I have managed to sort out part four of my problem with the code that follows. would there be anyway that I could modify it so that it would allow me to put the other 3 criteria in at the top of the text file and allow them to be modified? I’ve tried numerous methods of find/replace but they don’t seem to work with the code that I have so far.



	if gamewon is "p" then --P means the player won, I also have duplicate code for when the computer wins.
		(display dialog "Well done, you beat me!" with title "Noughts and Crosses!")
		my WriteLogplayerwin("Player won with x: " & (current date) & " -- text to be written to the file that displays the game board and how it was won, plus date/ time the win occurred
 " & squareone & " | " & squaretwo & " | " & squarethree & " 
 - + - + -
 " & squarefour & " | " & squarefive & " | " & squaresix & "
 - + - + -
 " & squareseven & " | " & squareeight & " | " & squarenine & "

")
end if

on WriteLogplayerwin(the_text)
	set this_story to the_text
	set this_file to (("/Users/John/Desktop/JARVIS/Stats/") as POSIX file) & "Noughts and Crosses Data"
	my write_to_fileplayerwin(this_story, this_file, true)
end WriteLogplayerwin

on write_to_fileplayerwin(this_data, target_file, append_data) -- (string, file path as string, boolean)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_fileplayerwin


I’ll admit the code that writes to the actual file I found on the net after hours of frustration, but I’ve only learnt about accessing and writing to files tonight… (forgive me! But I did write the actual game myself :slight_smile: )

If I’m missing something obvious then please let me know, but I’ve tried modifying this code with other research and become more confused as the night has gone on. If someone could explain to me the basics of what I’m trying to achieve I would be most grateful, most sites seem to only use variables in examples for modification, they don’t show how to modify text in another file…

Model: iMac
AppleScript: Version 2.4.3 (131.2)
Browser: Safari 534.56.5
Operating System: Mac OS X (10.8)

Hello John.

A couple of things needs to be clarifed.

How may players are you anticipating?

This task is a bit daunting for an Applescript Game, but is also puzzling and interesting. I think simple is good.

I propose that you use a folder for your files, one with totals, one for totals, and one for each player. That way you get rid of a lot of complexity, which is always good. As you can use the same file format, and the same handlers, for each of them. This scheme will also preseve any grand totals, For the computer and individual players.

The alternative is to construct some kind of database file format, you could use text item delimiters for this, and it isn’t really a big problem, it is just that using separate files, is so much smoother. :slight_smile:

You don’t have to count up players, seek for the playername inside the file, and change the contents of the file, as everything can be written out a fresh after each game.

I’ve written an small version of Naughts and crosses. Referring to the script I’ve made I give you some answers

What I should do to add this to my log is using those board masks and store those numbers with a mask to a file. So the file could be something like this:

won=1 loss=1 194;277;2012-12-04 10:10:00 277;194;2012-12-04 10:11:00 397;114;2012-12-04 10:12:00
Where in the log the first number is the player’s board mask and the second is the CPU’s board mask. The amount of log entries shows the amount of games played. The difference in the sum of won and loss and amount of log entries is 1 which means there is 1 game where the result was a draw.

Ahh. :smiley: I saw that other post!

I’ll toss in the Bayesian foundation for computing the probability of winning the next game, as it appeared in Sun Tzu some thousand years ago!

You just add one to the total number of wins, and two to the number of total games, divide the number of games won +1 by total games played +2, then you have the probability for the next game. :slight_smile:

Hi guys,

Thanks for the replies.

McUsrII, There will only be statistics for “player” and “computer”. Anyone who plays against the computer will be classed as Player. I’m not intending to have it as a proper game, just a minor part time project to add some interactivity to my home DLA.

The statistics file looks like this at the moment:

Player wins: 2 – Line 1

Jarvis wins: 0 – Line 3

Player won with x: Sunday, 2 December 2012 22:28:41 – First game played
x | o | 3

4 | x | 6

o | 8 | x

Player won with x: Sunday, 2 December 2012 22:42:32 – second game played
x | o | 3

o | x | 6

7 | 8 | x

I think I will use your idea of splitting the different statistics into separate files. This will help with gathering them up for the end leader board printing. These would be split into the following:

File 1 - Total Player wins
File 2 - Total Jarvis (Computer) wins
File 3 - Total Draws
File 4 - Previous win conditions

DJ Bazzie Wazzie, Wow, I’ve had a look at your version of the game, its a LOT more concise than mine! I like how you have added a difficulty level too. Well done! :smiley:

My original nooby question still stands though, how do I access the files to amend the amount of player/ computer wins or draws? I have to code sorted to add the win conditions but I cannot find a way to amend the other statistics (i.e. change payer wins from 2 to 3 when a game is won)?

John