Not exactly a pairing word game but i do need a piece of code to match up whatever the player has pressed with another item that the player pressed. Currently i have explored using XML, excel to trie and dawg but i do not know how to do about implementing it. Example of the game would be, player press on one word, say A and then press B, i need to be a able to test if A and B can match and if so, forms AB. and also i am able to match it to other words like AB and C gives me ABC. Anyone know how am i able to do this? And thanks very much, I am stuck for like a week now trying to find the ideal solution to the matching thing. Thanks.
Put your words into an array.
Then you just need to compare the active elements in the array to see if they are the same.
I’m really not following you here.
Is this for chaining glyphs into words?
For example:
c->
ca->
cat
car
Thanks thanks for reading. Anyways what i am trying to do is something like what JRavey said but since it is in chinese. I will give an example.
(Pardon me for using Chinese words) (Sorry the 2 words looks very similar but in fact there are not.)
Word 1: 骆
Word 2: 驼
Correct Words:骆驼 (Word 1 + Word 2) (Camel in Chinese)
Word 3: 狐
Word 4: 狸
Correct Words: 狐狸 (Word 3 + Word 4) (Fox in Chinese)
…And so on and so forth…
Then when player click word #1 and click word #2 it will tally to the correct words for #1 and #2 and likewise for #3 and #4.
How do i go about doing it then? Pardon me. I just got into scripting. Thanks for helping me.
You need a sorted list all possible matches first, because an sorted list is far less efficient for searching. It’s late though, so I will post more about it tomorrow. Lucky this problem really is language agnostic.
I am glad you are willing to help me. Thanks thanks. Sure sure.
I didn’t want to sleep without clearing up one bit about the basic problem being language agnostic.
One difference between, say German and Chinese, is that German has fewer letters/glyphs and some very long words; Chinese, on the other hand, has many glyphs but very short words. If these were both put into a binary tree for searching, you’d find that the German tree would be very deep and the Chinese tree to be very broad.
Obviously, this creates a search difference. But for your purposes, something in a game, I don’t think it will be too big of a deal.
Your first mission is to find your comprehensive list of Chinese words that you’ll use. I’m not too concerned if you are interested in Simplified or Traditional Chinese, or even both, as long as they are in a sorted list. All character sets have some form of sorting, which really is just a way of assigning an actual glyph to a series of bytes. The bytes are what you are going to sort on. If you were to properly sort everything by UTF which I’m pretty sure uses the radical order, then residual stroke count, then something else that doesn’t particularly matter to us. You are basically going to search by first character, then second, then third, until you have a match.
Luckily for you, .NET has the SortedList collection. You may want to consider one of these for your entire list of words, assuming you are only going to match on the word itself. I cannot over-emphasize how badly you’ll need to get this list together, then serialize it for later use. If you populate it each time at runtime from an unsorted list, you are going to end up with terrible load times as it’s basically resorting a very, very large array in your case. Again, lucky for you, you have access to SortedList.ContainsKey which has O(log n) mean time to result.
Anyway, you need:
- Your list of Chinese words
- To put this list into a SortedList
- Serialize the SortedList for later use
- Retrieve the serialized SortedList at runtime
- Use SortedList.ContainsKey() to find if your word is in the SortedList
That’s pretty much it. You don’t need to worry too much about the theory behind it, that’s mostly academic, but I think those steps provided are pretty good guidance. Somebody might have some esoteric way of doing it better, which I’m sure exists, but for most cases this is likely to be good enough.
OK, it’s 0100 here, but you have enough to get you started! Let us know how it goes.
Hey, JRavey. Thanks for the guide. Currently i am stuck. :c. Well i have put all the words into the sorted list but i have no idea what is serialization and how do i go about serializing the sorted list.
Anyways here’s the word list: (I put it bigger to see)
[SIZE="6"]
猫
狗
马
鸡
羊
猪
狐狸
骆驼
兔子
犀牛
斑马
猩猩
一石二鸟
望子成龙
画蛇添足
羊入虎口
鸡犬不宁
狼吞虎咽[/SIZE]
And here’s what my code roughly looks like.
(I am new to sorted list and collections and hashtables.So pardon me if i ask stupid question or do something wrongly).
void Start()
{
// Create and initializes a new SortedList
SortedList mySL = new SortedList();
mySL.Add("1", "猫");
mySL.Add("2", "狗");
mySL.Add("3", "马");
mySL.Add("4", "鸡");
mySL.Add("5", "羊");
mySL.Add("6", "猪");
mySL.Add("7", "狸");
mySL.Add("8", "骆");
mySL.Add("9", "驼");
mySL.Add("10", "兔");
mySL.Add("11", "子");
mySL.Add("12", "犀");
mySL.Add("13", "牛");
mySL.Add("14", "斑");
mySL.Add("15", "马");
mySL.Add("16", "猩");
mySL.Add("17", "猩");
mySL.Add("18", "一");
mySL.Add("19", "石");
mySL.Add("20", "二");
mySL.Add("21", "鸟");
mySL.Add("22", "望");
mySL.Add("23", "子");
mySL.Add("24", "成");
mySL.Add("25", "龙");
mySL.Add("26", "画");
mySL.Add("27", "蛇");
mySL.Add("28", "添");
mySL.Add("29", "足");
mySL.Add("30", "羊");
mySL.Add("31", "入");
mySL.Add("32", "虎");
mySL.Add("33", "口");
mySL.Add("34", "鸡");
mySL.Add("35", "犬");
mySL.Add("36", "不");
mySL.Add("37", "宁");
mySL.Add("38", "狼");
mySL.Add("39", "吞");
mySL.Add("40", "虎");
mySL.Add("41", "咽");
}
Basically i split all the words into single words (even the 2 and 4 words). So now i am stuck at serialization… Thanks. Take your time.
Note: For non-Chinese, the principle is the same, but use words if whichever language you want. I found UTF-16 works swimmingly with Unity, so save your files in that format. On OSX, I had to use TextEdit to do this by way of “Save As…”; Windows probably has something as well, Visual Studio probably does that easily but you’ll have to check the figure that out on your own.
Is that the entire list? Because if it is, you can just load it at runtime and not worry about serialization.
Notice that we need to add Systems.Collections.Generic to make the List available.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //You need this to have List
public class WordList : MonoBehaviour {
private List<string> wordList;
void Awake () {
wordList = new List<string>();
wordList.Add("猫");
wordList.Add("狗");
wordList.Add("马");
wordList.Add("鸡");
wordList.Add("羊");
wordList.Add("猪");
wordList.Add("狐狸");
wordList.Add("骆驼");
wordList.Add("兔子");
wordList.Add("犀牛");
wordList.Add("斑马");
wordList.Add("猩猩");
wordList.Add("一石二鸟");
wordList.Add("望子成龙");
wordList.Add("画蛇添足");
wordList.Add("羊入虎口");
wordList.Add("鸡犬不宁");
wordList.Add("狼吞虎咽");
}
//This could be a tiny bit tighter, but I wanted to make it easy to understand
public bool FindWord(string matchWord)
{
if(wordList.Contains(matchWord)){return true;}
else{return false;}
}
}
You could do this in a normal C# class if you’d like, but notice that we have no Start, Update, etc. We’re not going to need them, we just need to find this component and then query it. Here is a basic object which will find our WordList object then ask it for a sheep and a shark. The sheep should be found, the shark will not be.
Notice that we don’t need the Systems.Collections.Generic in this file, but you can put it if you’d like, it’s already in the project from the other one.
using UnityEngine;
using System.Collections;
public class WordCheckExample : MonoBehaviour {
private WordList _myWordList;
void Awake () {
_myWordList = (WordList)FindObjectOfType(typeof(WordList));
}
//These conditionals can be a tiny bit tighter, but I want you to understand them.
void Start () {
if(_myWordList.FindWord("羊") == true)
{Debug.Log("Found a sheep!");}
else
{Debug.Log("No sheep today, come back tomorrow!");}
if(_myWordList.FindWord("鲨鱼") == true)
{Debug.Log("Mind the sharks.");}
else
{Debug.Log("No sharks here.");}
}
}
If you have only the words listed, then you can hard code this. If you have thousands of words, which I thought you did, then you couldn’t possibly manually type all of them in a reasonable time and should automate the process.
Well thanks thanks. Yeah this is only the start of the list and it will expands as i go, so i am thinking of making it automate. Maybe reading from raw file stream or something. I have zero experience in file streams. And well anyways, thanks for helping me but there was something i would like to clarify. I am glad you helped me so much. But that was’t exactly what i wanted to do. Sorry if i wasn’t really clear but i need to compare the 2 words.
Say A is a character and B is another character and i needed a way to check if A B makes sense or not in this case in chinese words. And sometimes A C also makes sense and so forth. So i was thinking is it possible to like write all this in text files and parse them and compare.
I knew what you meant, all you need to do is aggregate the strings. If the player picks “兔” and then “子”, just add the strings together and then run FindWord(“兔子”).
In the meantime, you need to read up on how you can make this SortedList and then serialize it into a file. That’s really not very difficult.
Your first step though is getting your words in a simple text format for reading into your SortedList. When you have that list, read how to read a file line-by-line then take each line and add it into your sorted list. I would recommend only using subset of 20 or so, just to make sure you are doing right. When you’ve confirmed that you’re doing it right, then load everything into your SortedList and serialize it out to a new file.
Okay Thanks thanks. I got what you meant. I will go check it out. But it’s late for me here i am turning in for the night.
Zine,
Any progress?
Hello. Thanks. As of now, i have already implemented the serialization code. Cool for the links. It was really easy. But now i am having some problems with comparing the text and the bin files because i wanted to like compare if the text file have being modified and if so, re serialize the list instead of serializing the bin file every time i start the game.
Ah, just serialize the file once you update the list, not during playtime. When you start deserialize the list so the lookups happen in memory.
Ahh. Icic, yeah cuz right now, i have it check if a list.bin exist or not, if exist then skip reading the list.txt and deserialize the list.bin however i now am thinking what if i update my list.txt, my code doesn’t read “import” the list file and create a new bin file. So currently it is manual, so in order to update the bin, i have to delete the bin file and let it read the text to serialize. But overall, it is working the way i wanted it to be. Thanks JRavey.
Hi zine92, i am doing something similar to you. Just that for my case, i am doing the forming of chinese radicals. Are you doing it for fun or school work?
For my case, my games will be like 忄+ 亡= 忙.
How your games creation going? I just started mine. Are you using mysql for Unity?? how to add on to Unity?
To add on, when creating those scripts, where are we attaching those scripts to??
Would like to ask JRavey, is speech recognition possible to be added to the game? Does Unity support it? Would like to add speech recognition into the games. Thank you
Speech recognition is not native to Unity, which is understandable as it is a rather complex topic in itself. My recommendation on that is is find somebody who knows it well and make a plugin. Goodness knows that if one had a reliable .NET plugin for speech recognition on the asset store it would sell well. I have it in my car (Ford Sync) and it’s great, I know that a few games have it as well, but it is always fairly limited in what it understands.
You can add those scripts to any object as long as it makes sense to you and works.
Yeah. U r right. missy_pooh, for my project it is related to my course final year project and yeah i realized that adding the sides of the chinese words is just too complex and decided to change to adding words instead. And no i don’t use sql i used simple txt file to hold all my word list and use it to check if certain words exists. Sounds like u have a very complex game to make there.