tag:blogger.com,1999:blog-91295448250200183552024-03-14T00:22:04.742-07:00Algorithms and Java TechnologyThis blog was dedicated to everyone that want to learn Algorithms and Java Technology. I Hope you will enjoy learning and thanks for visiting my blog.Unknownnoreply@blogger.comBlogger17125tag:blogger.com,1999:blog-9129544825020018355.post-25966901456368093402017-01-22T07:36:00.002-08:002017-01-22T07:36:43.900-08:00TicTacToe AI with Minimax Algorithm<span class="fullpost">
<!-- HTML generated using hilite.me --></span>
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TicTacToeMinimax</span> <span style="color: #008800; font-weight: bold;">extends</span> TicTacToeAI <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #333333;">[]</span>nextMove<span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> token<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> enemy <span style="color: #333333;">=</span> inverse<span style="color: #333333;">(</span>token<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>isWin<span style="color: #333333;">(</span>enemy<span style="color: #333333;">))</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">};</span>
<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #333333;">[]</span>ret <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> best <span style="color: #333333;">=</span> <span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span> <span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>j<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span> j<span style="color: #333333;">)==</span>EMPTY<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
setBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">,</span>token<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #333333;">[]</span>res <span style="color: #333333;">=</span> nextMove<span style="color: #333333;">(</span>enemy<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> score <span style="color: #333333;">=</span> res<span style="color: #333333;">==</span><span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">?</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">:</span> <span style="color: #333333;">-</span>res<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>score <span style="color: #333333;">></span> best<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
best <span style="color: #333333;">=</span> score<span style="color: #333333;">;</span>
ret <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">,</span>best<span style="color: #333333;">};</span>
<span style="color: #333333;">}</span>
setBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">,</span>EMPTY<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> ret<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span class="fullpost">
</span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-48745551834968988362016-08-13T10:04:00.001-07:002023-12-19T14:36:02.686-08:00Online Tic Tac Toe Game With HTML5Hi, I have created online Tic Tac Toe game using HTML5.<span class="fullpost">
</span><br />
It comes with smart AI but not perfect, so there's a way that you can beat the computer.<br />
<br />
<div>
Play it here: <b><a href="https://algojava-8.firebaseapp.com/" target="_blank"><span style="font-size: large;">https://algojava-8.firebaseapp.com/</span></a></b><br />
<br />
<br /></div>
<div>
Screenshot from the game:</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-VsYE6e72Taw/V69S7ca1SeI/AAAAAAAAASQ/NX0oL8C7fvgByuesY9TqJ8e5euFBG4UbACLcB/s1600/tt.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/-VsYE6e72Taw/V69S7ca1SeI/AAAAAAAAASQ/NX0oL8C7fvgByuesY9TqJ8e5euFBG4UbACLcB/s1600/tt.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
The artificial intelligence logic based on my previous implementation using Java.<br />
I created this using Canvas and some Javascript code. Since Javascript very similiar with Java, it's not hard for me to implement this game using Javascript.<br />
In the future, I will post more games using HTML5.<br />
<br />
Please check my another post related Tic Tac Toe game.<br />
<br />
<ul>
<li><b><a href="https://algojava.blogspot.co.id/2012/05/tic-tac-toe-game-swingjava.html" target="_blank">Tic Tac Toe Game Swing Java</a></b></li>
<li><a href="https://algojava.blogspot.co.id/2008/12/how-to-create-tic-tac-toe-ai-java.html" target="_blank"><b>How To Create Tic Tac Toe AI</b></a></li>
</ul>
<br />
<br /></div>
<div>
<br /></div>
<div>
<b>Feel free to add your comment and question.</b></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-38940956560326780942016-08-09T06:59:00.003-07:002016-08-09T07:36:46.425-07:00Prime Number Program JavaHi, it's have been long time since my latest post about Luhn Algorithm.<span class="fullpost"></span><br />
Today, I will show you how to create prime number program with Java.<br />
<br />
<b><span style="font-size: large;"><br /></span></b>
<b><span style="font-size: large;">What is prime number?</span></b><br />
A prime number is a whole number greater than 1, whose only two whole-numberfactors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.<br />
<br />
<div>
<br /></div>
<div>
<b><span style="font-size: large;">How to check if given number X is prime or not?</span></b></div>
<div>
This method will return true if X is prime and return false if X is not prime.<br />
Time complexity for this algorithm are O( sqrt(X) ).</div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isPrime</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> x<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>x<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>x<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #333333;">||</span> x<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">3)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>x<span style="color: #333333;">%</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">0 || x%3==0</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> p<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">;</span> <span style="color: #333333;">(</span>p<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span> <span style="color: #333333;">*</span> <span style="color: #333333;">(</span>p<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span> <span style="color: #333333;"><=</span> x<span style="color: #333333;">;</span> p <span style="color: #333333;">+=</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>x <span style="color: #333333;">%</span> <span style="color: #333333;">(</span>p<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>x <span style="color: #333333;">%</span> <span style="color: #333333;">(</span>p<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<b><br /></b>
<b><span style="font-size: large;">How to generate prime number between A and B inclusive?</span></b><br />
A must be less than or equal B.<br />
Time complexity for this algorithm are O( range * sqrt(range) ), where range = B - A<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;">1
2
3
4
5
6
7
8
9</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">genPrimes</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> a<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> b<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> primes <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>Integer<span style="color: #333333;">>();</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>a<<span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">)</span> primes<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #333333;">2</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>a <span style="color: #333333;">%</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">)</span> a<span style="color: #333333;">++;</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> p<span style="color: #333333;">=</span>a<span style="color: #333333;">;</span> p<span style="color: #333333;"><=</span>b<span style="color: #333333;">;</span> p<span style="color: #333333;">+=</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>isPrime<span style="color: #333333;">(</span>p<span style="color: #333333;">))</span> primes<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>p<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> primes<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
If you want faster algorithm, please check my other post:<br />
<ul>
<li><a href="https://algojava.blogspot.co.id/2009/01/generate-prime-number-with-dynamic.html" target="_blank">Generate Prime Number With Dynamic Programming</a></li>
<li><a href="https://algojava.blogspot.co.id/2009/01/sieve-of-eratosthenes-c.html" target="_blank">Prime Number With Sieve Algorithm (C++)</a></li>
<li><a href="https://algojava.blogspot.co.id/2009/01/sieve-of-erasthothenes-optimize-memory.html" target="_blank">Sieve With Optimized Memory (Java)</a></li>
</ul>
<br />
<br />
<b>Feel free to add your question or comment. </b><br />
<b>For urgent request please send email to </b><b style="background-color: white; color: #666666; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px; line-height: 18.48px;"><a href="mailto:algojava8@gmail.com">algojava8@gmail.com</a></b>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-63073106835930942832014-12-14T06:57:00.000-08:002016-08-13T11:29:34.819-07:00Luhn Algorithm With JavaLuhn Algorithm is a method to check the validation of Credit Card.<br />
Almost all credit card in the world using this method for generating card number.<br />
<br />
This is my implementation using Java:
<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">LuhnValidator</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isValidCardNumber</span><span style="color: #333333;">(</span>String cardNumber<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>cardNumber<span style="color: #333333;">==</span><span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
cardNumber <span style="color: #333333;">=</span> cardNumber<span style="color: #333333;">.</span><span style="color: #0000cc;">trim</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>cardNumber<span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #333333;">))</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> res <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">boolean</span> odd<span style="color: #333333;">=</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span>cardNumber<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">()-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span><span style="color: #333333;">></span><span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;">--)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">char</span> c <span style="color: #333333;">=</span> cardNumber<span style="color: #333333;">.</span><span style="color: #0000cc;">charAt</span><span style="color: #333333;">(</span>i<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>c><span style="color: #333333;">=</span><span style="color: #0044dd;">'0'</span> <span style="color: #333333;">&&</span> c<<span style="color: #333333;">=</span><span style="color: #0044dd;">'9'</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> v <span style="color: #333333;">=</span> c <span style="color: #333333;">-</span> <span style="color: #0044dd;">'0'</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(!</span>odd<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
v <span style="color: #333333;">*=</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">;</span>
v <span style="color: #333333;">=</span> <span style="color: #333333;">(</span>v<span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">)</span> <span style="color: #333333;">+</span> <span style="color: #333333;">(</span>v<span style="color: #333333;">%</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
odd <span style="color: #333333;">=</span> <span style="color: #333333;">!</span>odd<span style="color: #333333;">;</span>
res <span style="color: #333333;">+=</span> v<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">// skip space</span>
<span style="color: #008800; font-weight: bold;">else</span> <span style="color: #0066bb; font-weight: bold;">if</span><span style="color: #333333;">(</span>c<span style="color: #333333;">!=</span><span style="color: #0044dd;">' '</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// other character is not valid</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #333333;">(</span>res<span style="color: #333333;">%</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">)</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String <span style="color: #333333;">[]</span>args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>LuhnValidator<span style="color: #333333;">.</span><span style="color: #0000cc;">isValidCardNumber</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>LuhnValidator<span style="color: #333333;">.</span><span style="color: #0000cc;">isValidCardNumber</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">" "</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>LuhnValidator<span style="color: #333333;">.</span><span style="color: #0000cc;">isValidCardNumber</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"4111 1111 1111 1111"</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>LuhnValidator<span style="color: #333333;">.</span><span style="color: #0000cc;">isValidCardNumber</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"4124123143215674"</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>LuhnValidator<span style="color: #333333;">.</span><span style="color: #0000cc;">isValidCardNumber</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"4184126643215675"</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-9129544825020018355.post-8295528883234890232012-05-13T06:49:00.000-07:002023-12-18T04:30:54.663-08:00Tic Tac Toe Game (Swing,Java)<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-oXbydUsHKGs/VztcE5J92yI/AAAAAAAAARE/yl6sinF4BjMMhBnnrRAJdU5d5rfgmlM1wCLcB/s1600/tic-tac-toe.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://2.bp.blogspot.com/-oXbydUsHKGs/VztcE5J92yI/AAAAAAAAARE/yl6sinF4BjMMhBnnrRAJdU5d5rfgmlM1wCLcB/s320/tic-tac-toe.png" width="317" /></a></div>
<br />
<br />
Here I present a simple TicTacToe Program. <br />
You can download my code and play in your computer.<br />
<br />
<a name='more'></a>Download Source Code:<br />
<a href="https://github.com/timotius/tic-tac-toe/archive/master.zip" target="_blank">TicTacToe</a><br />
<br />
<span style="font-size: x-large;"><b>Save to file: TicTacToeMain.java</b></span><br />
<br />
<pre>import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TicTacToeMain extends JFrame implements ActionListener {
private JButton [][]buttons = new JButton[3][3];
private JButton playButton = new JButton("Play");
private JLabel statusLabel = new JLabel("");
private TicTacToeAI game = null;
private int human = 0;
private int computer = 0;
private boolean isPlay = false;
private String []chars=new String[]{"","X","O"};
private void setStatus(String s) {
statusLabel.setText(s);
}
private void setButtonsEnabled(boolean enabled) {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
buttons[i][j].setEnabled(enabled);
if(enabled) buttons[i][j].setText(" ");
}
}
public TicTacToeMain() {
setTitle("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JPanel centerPanel = new JPanel(new GridLayout(3,3));
Font font = new Font("Arial",Font.BOLD, 32);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
buttons[i][j] = new JButton(" ");
buttons[i][j].setFont(font);
buttons[i][j].addActionListener(this);
buttons[i][j].setFocusable(false);
centerPanel.add(buttons[i][j]);
}
playButton.addActionListener(this);
JPanel northPanel = new JPanel();
northPanel.add(statusLabel);
JPanel southPanel = new JPanel();
southPanel.add(playButton);
setStatus("Click 'Play' To Start");
setButtonsEnabled(false);
add(northPanel,"North");
add(centerPanel,"Center");
add(southPanel,"South");
setSize(300,300);
// I'm lazy to implement the correct way
setLocationRelativeTo(null);
}
public static void main(String []args) {
new TicTacToeMain().setVisible(true);
}
private void computerTurn() {
if(!isPlay) return;
int []pos = game.nextMove(computer);
if(pos!=null) {
int i = pos[0];
int j = pos[1];
buttons[i][j].setText(chars[computer]);
game.setBoardValue(i,j,computer);
}
checkState();
}
private void gameOver(String s) {
setStatus(s);
setButtonsEnabled(false);
isPlay = false;
}
private void checkState() {
if(game.isWin(human)) {
gameOver("Congratulations, You've Won!");
}
if(game.isWin(computer)) {
gameOver("Sorry, You Lose!");
}
if(game.nextMove(human)==null && game.nextMove(computer)==null) {
gameOver("Draw, Click 'Play' For Rematch!");
}
}
private void click(int i,int j) {
if(game.getBoardValue(i,j)==TicTacToeAI.EMPTY) {
buttons[i][j].setText(chars[human]);
game.setBoardValue(i,j,human);
checkState();
computerTurn();
}
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==playButton) {
play();
}else {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(event.getSource()==buttons[i][j])
click(i,j);
}
}
private void play() {
game = new TicTacToeAI();
human = TicTacToeAI.ONE;
computer = TicTacToeAI.TWO;
setStatus("Your Turn");
setButtonsEnabled(true);
isPlay = true;
}
}
</pre>
<span style="font-size: x-large;"><b>Save to file: TicTacToeAI.java</b></span><br />
<br />
<pre>public class TicTacToeAI {
/* the board */
private int board[][];
/* empty */
public static final int EMPTY = 0;
/* player one */
public static final int ONE = 1;
/* player two */
public static final int TWO = 2;
public TicTacToeAI() {
board = new int[3][3];
}
/* get the board value for position (i,j) */
public int getBoardValue(int i,int j) {
if(i < 0 || i >= 3) return EMPTY;
if(j < 0 || j >= 3) return EMPTY;
return board[i][j];
}
/* set the board value for position (i,j) */
public void setBoardValue(int i,int j,int token) {
if(i < 0 || i >= 3) return;
if(j < 0 || j >= 3) return;
board[i][j] = token;
}
/* calculate the winning move for current token */
public int []nextWinningMove(int token) {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(getBoardValue(i, j)==EMPTY) {
board[i][j] = token;
boolean win = isWin(token);
board[i][j] = EMPTY;
if(win) return new int[]{i,j};
}
return null;
}
public int inverse(int token) {
return token==ONE ? TWO : ONE;
}
/* calculate the best move for current token */
public int []nextMove(int token) {
/* lucky position in the center of board*/
if(getBoardValue(1, 1)==EMPTY) return new int[]{1,1};
/* if we can move on the next turn */
int winMove[] = nextWinningMove(token);
if(winMove!=null) return winMove;
/* choose the move that prevent enemy to win */
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(getBoardValue(i, j)==EMPTY)
{
board[i][j] = token;
boolean ok = nextWinningMove(inverse(token)) == null;
board[i][j] = EMPTY;
if(ok) return new int[]{i,j};
}
/* choose available move */
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(getBoardValue(i, j)==EMPTY)
return new int[]{i,j};
/* no move is available */
return null;
}
/* determine if current token is win or not win */
public boolean isWin(int token) {
final int DI[]={-1,0,1,1};
final int DJ[]={1,1,1,0};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
/* we skip if the token in position(i,j) not equal current token */
if(getBoardValue(i, j)!=token) continue;
for(int k=0;k<4;k++) {
int ctr = 0;
while(getBoardValue(i+DI[k]*ctr, j+DJ[k]*ctr)==token) ctr++;
if(ctr==3) return true;
}
}
return false;
}
} </pre>
<br />
Unknownnoreply@blogger.com18tag:blogger.com,1999:blog-9129544825020018355.post-87708231961144883892009-01-09T23:28:00.000-08:002016-08-12T21:55:09.946-07:00Generate Prime Numbers With "Dynamic Programming"Here I present one technique to generate the first N prime numbers with dynamic programming. Dynamic Programming is a paradigm for designing algorithms where the next solution was taken from previous solution.<br />
<br />
The memory complexity is O(N).<br />
The time complexity is O(N*C), where C is the number of prime between 2 and sqrt(N) (inclusive).<br />
<br />
Advantages:<br />
* flexibility (generate first N prime numbers).<br />
* can check prime until N^2 with complexity O(C).<br />
* in my computer can generate first 200,000 prime numbers below 1 secs.<br />
<br />
Disadvantages:<br />
* slow when N is too large.<br />
<br />
<a name='more'></a><br />
<br />
<span class="fullpost"><br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">PrimeDP</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">int</span> primes<span style="color: #333333">[];</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">boolean</span> <span style="color: #0066BB; font-weight: bold">isPrime</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> x<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>x<span style="color: #333333"><</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">false</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> k<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">;</span>k <span style="color: #333333"><</span> primes<span style="color: #333333">.</span><span style="color: #0000CC">length</span> <span style="color: #333333">&&</span> primes<span style="color: #333333">[</span>k<span style="color: #333333">]*</span>primes<span style="color: #333333">[</span>k<span style="color: #333333">]</span> <span style="color: #333333"><=</span> x<span style="color: #333333">;</span>k<span style="color: #333333">++)</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>x<span style="color: #333333">%</span>primes<span style="color: #333333">[</span>k<span style="color: #333333">]==</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">false</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">true</span><span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">getPrime</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> index<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">return</span> primes<span style="color: #333333">[</span>index<span style="color: #333333">];</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #0066BB; font-weight: bold">PrimeDP</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> n<span style="color: #333333">)</span> <span style="color: #333333">{</span>
primes <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">[</span>n<span style="color: #333333">];</span>
primes<span style="color: #333333">[</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">]</span> <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">;</span>
primes<span style="color: #333333">[</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">]</span> <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">3</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> k<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">,</span>i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">6</span><span style="color: #333333">;</span>k <span style="color: #333333"><</span> n<span style="color: #333333">;</span>i<span style="color: #333333">+=</span><span style="color: #0000DD; font-weight: bold">6</span><span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>isPrime<span style="color: #333333">(</span>i<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">))</span> primes<span style="color: #333333">[</span>k<span style="color: #333333">++]=</span>i<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>k <span style="color: #333333"><</span> n <span style="color: #333333">&&</span> isPrime<span style="color: #333333">(</span>i<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">))</span>
primes<span style="color: #333333">[</span>k<span style="color: #333333">++]=</span>i<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span><span style="color: #333333">(</span>String <span style="color: #333333">[]</span>args<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">long</span> a <span style="color: #333333">=</span> System<span style="color: #333333">.</span><span style="color: #0000CC">currentTimeMillis</span><span style="color: #333333">();</span>
<span style="color: #333399; font-weight: bold">int</span> n <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">200000</span><span style="color: #333333">;</span>
PrimeDP p <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> PrimeDP<span style="color: #333333">(</span>n<span style="color: #333333">);</span>
<span style="color: #333399; font-weight: bold">long</span> b <span style="color: #333333">=</span> System<span style="color: #333333">.</span><span style="color: #0000CC">currentTimeMillis</span><span style="color: #333333">();</span>
System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"time = "</span> <span style="color: #333333">+</span> <span style="color: #333333">(</span>b<span style="color: #333333">-</span>a<span style="color: #333333">)</span> <span style="color: #333333">+</span> <span style="background-color: #fff0f0">" ms."</span><span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
</pre></td></tr></table></div>
<span class="fullpost">
</span>Unknownnoreply@blogger.com7tag:blogger.com,1999:blog-9129544825020018355.post-87658295730518483342009-01-04T22:25:00.000-08:002016-08-09T07:41:14.575-07:00Sieve Of Erasthothenes (Optimize Memory Version)<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">Sieve</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">private</span> <span style="color: #333399; font-weight: bold">int</span> prime<span style="color: #333333">[];</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #333399; font-weight: bold">boolean</span> <span style="color: #0066BB; font-weight: bold">isPrime</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> x<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>x<span style="color: #333333">==</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">true</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>x<span style="color: #333333"><</span><span style="color: #0000DD; font-weight: bold">2</span> <span style="color: #333333">||</span> x<span style="color: #333333">%</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">==</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">return</span> <span style="color: #008800; font-weight: bold">false</span><span style="color: #333333">;</span>
x<span style="color: #333333">>>=</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #333333">(</span>prime<span style="color: #333333">[</span>x<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">5</span><span style="color: #333333">]</span> <span style="color: #333333">&</span> <span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333"><<(</span>x<span style="color: #333333">&</span><span style="color: #0000DD; font-weight: bold">31</span><span style="color: #333333">)))</span> <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">;</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #0066BB; font-weight: bold">Sieve</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> n<span style="color: #333333">)</span> <span style="color: #333333">{</span>
prime <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> <span style="color: #333399; font-weight: bold">int</span><span style="color: #333333">[(</span>n<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">6</span><span style="color: #333333">)+</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">];</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">3</span><span style="color: #333333">;</span>i<span style="color: #333333">*</span>i<span style="color: #333333"><=</span>n<span style="color: #333333">;</span>i<span style="color: #333333">+=</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">int</span> hi <span style="color: #333333">=</span> i<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">;</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span> <span style="color: #333333">(</span>prime<span style="color: #333333">[</span>hi<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">5</span><span style="color: #333333">]</span> <span style="color: #333333">&</span> <span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333"><<(</span>hi<span style="color: #333333">&</span><span style="color: #0000DD; font-weight: bold">31</span><span style="color: #333333">)))</span> <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> j<span style="color: #333333">=</span>i<span style="color: #333333">*</span>i<span style="color: #333333">;</span>j<span style="color: #333333"><=</span>n<span style="color: #333333">;</span>j<span style="color: #333333">+=</span>i<span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">((</span>j<span style="color: #333333">&</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">)!=</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">int</span> hj <span style="color: #333333">=</span> j<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">;</span>
prime<span style="color: #333333">[</span>hj<span style="color: #333333">>></span><span style="color: #0000DD; font-weight: bold">5</span><span style="color: #333333">]</span> <span style="color: #333333">|=</span> <span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333"><<(</span>hj<span style="color: #333333">&</span><span style="color: #0000DD; font-weight: bold">31</span><span style="color: #333333">));</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">public</span> <span style="color: #008800; font-weight: bold">static</span> <span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">main</span><span style="color: #333333">(</span>String <span style="color: #333333">[]</span>args<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #333399; font-weight: bold">long</span> start <span style="color: #333333">=</span> System<span style="color: #333333">.</span><span style="color: #0000CC">currentTimeMillis</span><span style="color: #333333">();</span>
Sieve sieve <span style="color: #333333">=</span> <span style="color: #008800; font-weight: bold">new</span> Sieve<span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">10000000</span><span style="color: #333333">);</span>
<span style="color: #333399; font-weight: bold">long</span> finish <span style="color: #333333">=</span> System<span style="color: #333333">.</span><span style="color: #0000CC">currentTimeMillis</span><span style="color: #333333">();</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">;</span>i<span style="color: #333333"><</span><span style="color: #0000DD; font-weight: bold">100</span><span style="color: #333333">;</span>i<span style="color: #333333">++)</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>sieve<span style="color: #333333">.</span><span style="color: #0000CC">isPrime</span><span style="color: #333333">(</span>i<span style="color: #333333">))</span> System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">print</span><span style="color: #333333">(</span>i <span style="color: #333333">+</span> <span style="background-color: #fff0f0">" "</span><span style="color: #333333">);</span>
System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">""</span><span style="color: #333333">);</span>
System<span style="color: #333333">.</span><span style="color: #0000CC">out</span><span style="color: #333333">.</span><span style="color: #0000CC">println</span><span style="color: #333333">(</span><span style="background-color: #fff0f0">"Time : "</span> <span style="color: #333333">+</span> <span style="color: #333333">(</span>finish<span style="color: #333333">-</span>start<span style="color: #333333">)/</span><span style="color: #6600EE; font-weight: bold">1000.0</span> <span style="color: #333333">+</span> <span style="background-color: #fff0f0">" secs"</span><span style="color: #333333">);</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
</pre></td></tr></table></div>
Unknownnoreply@blogger.com5tag:blogger.com,1999:blog-9129544825020018355.post-66004791146121664892009-01-04T18:29:00.000-08:002009-01-04T18:44:04.752-08:003: Bit Manipulation (check the bit and flip the bit)In the previous post, we already learn how to set the bit and clear the bit. To check whether the k-th bit is 0 or 1 we can use & operator. To flip(change from 0 to 1 or 1 to 0) the bit simply use ^ (XOR) operator. This is the last series from "Trilogy Of Bit Manipulation".<br /><span class="fullpost"><br /><pre><br />int x = 10; // 00001010<br /><br />// check the bit<br />if( (x & (1<<3)) != 0 )<br /> printf("3-th bit is 1");<br />else<br /> printf("3-th bit is 0");<br /><br />// flip the bit<br />x = x ^ (1<<3); // long version<br />printf("%d\n",x); // x = 2 = 00000010<br /><br />// flip the bit again<br />x ^= (1<<3); // short version<br />printf("%d\n",x); // x = 10 = 00001010<br /></pre><br /></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-76351021086772448542009-01-03T23:13:00.001-08:002009-01-03T23:32:30.332-08:002: Bit Manipulation (clear the bit)In the first series you have learn how to set the bit, now you will learn how to clear the bit.<br />This is the second series from "Trilogy Of Bit Manipulation".<br /><br /><pre><br />int x = 0;<br />x |= (1<<2); // x = 00000100<br />x |= (1<<4);<br /></pre><br /><span class="fullpost"><br />To clear the k-th bit, we can use & and ~ operator.<br />x = x & ~(1<<2); // I clear the 2-th bit.<br /><span style="font-weight: bold;">How does it work ?</span><br />& is AND operation:<br />1 & 1 = 1<br />1 & 0 = 0<br />0 & 1 = 0<br />0 & 0 = 0<br /><br />~ is NEGATE operation:<br />~ 1 = 0<br />~ 0 = 1<br /><br /><pre><br />x = x & ~(1<<2); // long version<br />x &= ~(1<<2); // short version<br /></pre><br /><br />initial x = 17 = 00010001<br />and I want to clear the 0-th bit.<br /><br />(1<<0) = 00000001<br />~(1<<0) = 11111110<br /><br />00010001<br />11111110<br />-------- AND operation<br />00010000 (the 0-th bit is clear)<br /><br /></span>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-9129544825020018355.post-11698018556949847072009-01-03T22:42:00.000-08:002009-01-03T23:27:27.930-08:001: Bit Manipulation (set the bit)In 32-bit Compiler, int size is 4 byte (32 bit). <br />Today we will play with bit ( I will type the last 8 bit rather than 32 bit to simplify my explanation).<br />This is the first series from "Trilogy Of Bit Manipulation". <br /><br /><pre><br />int x = 0; <br />//The bit value is : 00000000 (all the bit is 0).<br /></pre><br /><span class="fullpost"><br /><pre> <br />x = x | (1<<0); // set the 0-th bit from right. <br />// The bit value is : 00000001 (the last bit is 0).<br /></pre><br /><pre> <br />x = x | (1<<2); // set the 2-th bit from right. <br />// The bit value is : 00000101.<br /></pre><br /><span style="font-weight:bold;">How does it work ?</span><br /><br />| is OR operation :<br />1 or 1 = 1<br />1 or 0 = 1<br />0 or 1 = 1<br />0 or 0 = 0<br /><br /><< is Shift Left operation :<br />1<<0 = 1 = 00000001<br />1<<1 = 2 = 00000010<br />1<<2 = 4 = 00000100<br />1<<3 = 8 = 00001000<br /><br />So when I have x = 10 = 00001010<br /><br />And I want to set the 5-th bit: <br />x = x | (1<<5); // long version<br />x |= (1<<5); // short version<br /><br /><pre><br />x = 10 = 00001010<br />1<<5 = 32 = 00100000<br />OR operation-----------<br /> 00101010<br /></pre><br /></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-64883280859068267402009-01-03T07:26:00.000-08:002016-08-13T11:22:28.297-07:00Sudoku Solver (C++)What is Sudoku ? <br />
Sudoku is a logic-based number-placement puzzle. The objective is to fill a 9×9 grid so that each column, each row, and each of the nine 3×3 boxes (also called blocks or regions) contains the digits from 1 to 9 only one time each. The puzzle setter provides a partially completed grid.<br />
<span class="fullpost"><br />Here I present a solution to solve sudoku with a simple backtracking. I use bitmask to mark the number already used in each row, column and block. I can't estimate what is the time complexity of my code, but I guarantee it's fast enough (< 1 secs on my laptop). There is another efficient solution using dancing link but more harder to code.<br /><br />Here's the full c++ code:</span><br />
<pre><span class="fullpost">
<!-- HTML generated using hilite.me --><div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #557799;">#include "stdio.h"</span>
<span style="color: #333399; font-weight: bold;">int</span> board[<span style="color: #0000dd; font-weight: bold;">9</span>][<span style="color: #0000dd; font-weight: bold;">9</span>];
<span style="color: #333399; font-weight: bold;">int</span> fi[<span style="color: #0000dd; font-weight: bold;">9</span>]; <span style="color: #888888;">// forbidden row</span>
<span style="color: #333399; font-weight: bold;">int</span> fj[<span style="color: #0000dd; font-weight: bold;">9</span>]; <span style="color: #888888;">// forbidden column</span>
<span style="color: #333399; font-weight: bold;">int</span> fk[<span style="color: #0000dd; font-weight: bold;">9</span>]; <span style="color: #888888;">// forbidden block</span>
<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">solve</span>(<span style="color: #333399; font-weight: bold;">int</span> p) {
<span style="color: #008800; font-weight: bold;">if</span>(p<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">0</span>)
{
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> a<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;a<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">9</span>;a<span style="color: #333333;">++</span>) fi[a]<span style="color: #333333;">=</span>fj[a]<span style="color: #333333;">=</span>fk[a]<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">9</span>;i<span style="color: #333333;">++</span>)
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">9</span>;j<span style="color: #333333;">++</span>)
<span style="color: #008800; font-weight: bold;">if</span>(board[i][j]<span style="color: #333333;">!=</span><span style="color: #0000dd; font-weight: bold;">0</span>)
{
<span style="color: #333399; font-weight: bold;">int</span> x <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;"><<</span>(board[i][j]<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span>);
<span style="color: #333399; font-weight: bold;">int</span> k <span style="color: #333333;">=</span> i <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #333333;">+</span> (j <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">3</span>);
fi[i] <span style="color: #333333;">|=</span> x;
fj[j] <span style="color: #333333;">|=</span> x;
fk[k] <span style="color: #333333;">|=</span> x;
}
}
<span style="color: #008800; font-weight: bold;">if</span>(p<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">81</span>) <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> p <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">9</span>; <span style="color: #888888;">// calculate row position</span>
<span style="color: #333399; font-weight: bold;">int</span> j <span style="color: #333333;">=</span> p <span style="color: #333333;">%</span> <span style="color: #0000dd; font-weight: bold;">9</span>; <span style="color: #888888;">// calculate column position</span>
<span style="color: #333399; font-weight: bold;">int</span> k <span style="color: #333333;">=</span> i <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #333333;">+</span> (j <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">3</span>); <span style="color: #888888;">// calculate block position</span>
<span style="color: #008800; font-weight: bold;">if</span>(board[i][j]<span style="color: #333333;">!=</span><span style="color: #0000dd; font-weight: bold;">0</span>) <span style="color: #008800; font-weight: bold;">return</span> solve(p<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>);
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> no<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span>;no<span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">9</span>;no<span style="color: #333333;">++</span>)
{
<span style="color: #333399; font-weight: bold;">int</span> x <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;"><<</span>(no<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span>);
<span style="color: #888888;">// check the bit</span>
<span style="color: #008800; font-weight: bold;">if</span>(fi[i] <span style="color: #333333;">&</span> x) <span style="color: #008800; font-weight: bold;">continue</span>;
<span style="color: #008800; font-weight: bold;">if</span>(fj[j] <span style="color: #333333;">&</span> x) <span style="color: #008800; font-weight: bold;">continue</span>;
<span style="color: #008800; font-weight: bold;">if</span>(fk[k] <span style="color: #333333;">&</span> x) <span style="color: #008800; font-weight: bold;">continue</span>;
<span style="color: #888888;">// mark the bit</span>
fi[i] <span style="color: #333333;">|=</span> x;
fj[j] <span style="color: #333333;">|=</span> x;
fk[k] <span style="color: #333333;">|=</span> x;
board[i][j] <span style="color: #333333;">=</span> no;
<span style="color: #888888;">// recurse the next level</span>
<span style="color: #008800; font-weight: bold;">if</span>(solve(p<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>)<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">1</span>) <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
board[i][j] <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #888888;">// clear the bit</span>
fi[i] <span style="color: #333333;">&=</span> <span style="color: #333333;">~</span>x;
fj[j] <span style="color: #333333;">&=</span> <span style="color: #333333;">~</span>x;
fk[k] <span style="color: #333333;">&=</span> <span style="color: #333333;">~</span>x;
}
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
}
<span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">main</span>() {
<span style="color: #888888;">// fill the board in here</span>
<span style="color: #888888;">// end of fill the board </span>
<span style="color: #888888;">// print the result</span>
<span style="color: #008800; font-weight: bold;">if</span>(solve(<span style="color: #0000dd; font-weight: bold;">0</span>)<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">0</span>) puts(<span style="background-color: #fff0f0;">"no solution"</span>);
<span style="color: #008800; font-weight: bold;">else</span> {
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">9</span>;i<span style="color: #333333;">++</span>,puts(<span style="background-color: #fff0f0;">""</span>))
<span style="color: #008800; font-weight: bold;">for</span>(<span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">9</span>;j<span style="color: #333333;">++</span>)
printf(<span style="background-color: #fff0f0;">"%d"</span>,board[i][j]);
}
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
}
</pre>
</td></tr>
</tbody></table>
</div>
<span class="fullpost">
</span></span></pre>
Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-9129544825020018355.post-6462516375479962182009-01-02T11:25:00.001-08:002009-01-03T06:47:04.209-08:00How to create TextField that only accept letter (Java)If you use a normal JTextField then all character(s) can be accepted. But sometimes we need a textfield that only accept letter ('a'-'z' and 'A'-'Z'). <br />I Hope this JLetterField.java can solve your problem.<br /><span class="fullpost"><br /><pre><br />import javax.swing.JTextField;<br />import javax.swing.text.AttributeSet;<br />import javax.swing.text.BadLocationException;<br />import javax.swing.text.Document;<br />import javax.swing.text.PlainDocument;<br /><br />public class JLetterField<br />extends JTextField<br />{<br /> public JLetterField(int cols) {<br /> super("",cols);<br /> }<br /><br /> protected Document createDefaultModel() {<br /> return new LetterDocument();<br /> }<br /> <br /> class LetterDocument extends PlainDocument {<br /> public void insertString(int offs,String str,AttributeSet a)<br /> throws BadLocationException<br /> {<br /> if(str==null) return;<br /> char [] before = str.toCharArray();<br /> String after = "";<br /> for(int i=0;i < before.length;i++)<br /> {<br /> if( (before[i]>='a' && before[i]<='z') || (before[i]>='A' && before[i]<='Z') )<br /> after += before[i];<br /> }<br /> super.insertString(offs,after,a);<br /> }<br /> } <br />}<br /></pre><br /></span>Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-9129544825020018355.post-34787128291958516202009-01-02T00:29:00.000-08:002016-08-12T21:53:21.924-07:00Sieve Of Eratosthenes (C++)In mathematics, the Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer.<br />
<br />
The Algorithm:<br />
1. Create a contiguous list of numbers from two to some highest number n.<br />
2. Strike out from the list all multiples of two (4, 6, 8 etc.).<br />
3. The list's next number that has not been struck out is a prime number.<br />
4. Strike out from the list all multiples of the number you identified in the previous step.<br />
5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list).<br />
6. All the remaining numbers in the list are prime.<br />
<span class="fullpost"><br />The C++ code:</span><br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #557799">#include "stdio.h"</span>
<span style="color: #557799">#include "conio.h"</span>
<span style="color: #008800; font-weight: bold">const</span> <span style="color: #333399; font-weight: bold">int</span> SIZE <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">10000</span>;
<span style="color: #333399; font-weight: bold">char</span> f[SIZE <span style="color: #333333">+</span> <span style="color: #0000DD; font-weight: bold">1</span>];
<span style="color: #333399; font-weight: bold">int</span> <span style="color: #0066BB; font-weight: bold">main</span>() {
<span style="color: #888888">// 0 and 1 is not prime</span>
f[<span style="color: #0000DD; font-weight: bold">0</span>]<span style="color: #333333">=</span>f[<span style="color: #0000DD; font-weight: bold">1</span>]<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">1</span>;
<span style="color: #888888">// every even number (except 2) is not prime</span>
<span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">4</span>;i<span style="color: #333333"><=</span>SIZE;i<span style="color: #333333">+=</span><span style="color: #0000DD; font-weight: bold">2</span>) f[i]<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">1</span>;
<span style="color: #888888">// iterate every odd number (start from 3) </span>
<span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">3</span>;i<span style="color: #333333">*</span>i<span style="color: #333333"><=</span>SIZE;i<span style="color: #333333">+=</span><span style="color: #0000DD; font-weight: bold">2</span>)
{
<span style="color: #888888">// if the current number is prime then the multiple is not prime </span>
<span style="color: #008800; font-weight: bold">if</span>(f[i]<span style="color: #333333">==</span><span style="color: #0000DD; font-weight: bold">0</span>)
{
<span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> j<span style="color: #333333">=</span>i<span style="color: #333333">*</span>i;j<span style="color: #333333"><=</span>SIZE;j<span style="color: #333333">+=</span>i)
f[j]<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">1</span>;
}
}
<span style="color: #888888">// display prime numbers between 0 and 100</span>
<span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> i<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">0</span>;i<span style="color: #333333"><=</span><span style="color: #0000DD; font-weight: bold">100</span>;i<span style="color: #333333">++</span>)
<span style="color: #008800; font-weight: bold">if</span>(f[i]<span style="color: #333333">==</span><span style="color: #0000DD; font-weight: bold">0</span>) printf(<span style="background-color: #fff0f0">"%d</span><span style="color: #666666; font-weight: bold; background-color: #fff0f0">\n</span><span style="background-color: #fff0f0">"</span>,i);
getch();
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #0000DD; font-weight: bold">0</span>;
}
</pre></td></tr></table></div>
<span class="fullpost"><br /></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-9129544825020018355.post-76535067708703213452009-01-01T09:54:00.000-08:002009-01-01T10:05:58.742-08:00How to create replaceAll() (C++) ?Happy New Year World, this is my first post in 2009. This time I will tell about how to create replaceAll() function. This function are used to replace part of original string that contains old value to new value. The idea just iterate the string you want to replace and take the substring from each index then compare to the value you want to replace.<br /><span class="fullpost"><br />You can see the fullcode below :<br /><pre><br />#include "iostream"<br />#include "string"<br />using namespace std;<br /><br />string replaceAll(string text,string from,string to) {<br /> string result = "";<br /> int i = 0;<br /> while(i < text.size()) <br /> {<br /> if(i+from.size() <= text.size() && text.substr(i,from.size())==from )<br /> {<br /> result += to;<br /> i += from.size();<br /> }<br /> else <br /> {<br /> result.push_back(text[i]);<br /> i++;<br /> }<br /> }<br /> return result;<br />}<br /><br />int main() {<br /> string a = "C++ is easy";<br /> cout << a << endl;<br /> a = replaceAll(a,"C++ is","C++ and Java are");<br /> cout << a << endl; <br /> return 0;<br />}<br /></pre><br /><br /></span>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-9129544825020018355.post-61550063240834136852008-12-30T01:23:00.000-08:002008-12-30T22:54:10.781-08:00Terbilang (Java)Here's Terbilang.java, it's to convert decimal notation into word (Indonesia).<br /><br />Example:<br /><br />100 ==> one hundred (seratus)<br />1020 ==> one thousand and twenty (seribu dua puluh)<br /><br />So it's very useful for people that want to learning Indonesia ^^.<br /><br /><span class="fullpost"><br /><pre><br />public class Terbilang { <br /> public static String bilang(long uang) {<br /> String nama[]={"nol","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan"};<br /> String besar[]={"triliun","milyar","juta","ribu",""}; <br /> if(uang==0) return nama[0];<br /> long p = 1000000000000l;<br /> String hasil = "";<br /> for(int i=0;i < besar.length;i++,p /= 1000)<br /> {<br /> if(uang < p) continue;<br /> long temp = uang / p; <br /> boolean seribu = p==1000;<br /> if(temp>=100) {<br /> hasil += nama[(int)temp/100] + " ratus ";<br /> temp %= 100;<br /> seribu = false;<br /> }<br /> if(temp>=11 && temp<=19) {<br /> hasil += nama[(int)temp-10] + " belas ";<br /> temp = 0;<br /> seribu = false;<br /> }<br /> if(temp>=10) {<br /> hasil += nama[(int)temp/10] + " puluh ";<br /> temp %= 10;<br /> }<br /> if(temp > 0) {<br /> if(seribu && temp==1)<br /> hasil += "se";<br /> else<br /> hasil += nama[(int)temp] + " "; <br /> }<br /> uang %= p;<br /> hasil += besar[i] + " ";<br /> }<br /> hasil=hasil.replaceAll("satu ratus", "seratus");<br /> hasil=hasil.replaceAll("satu belas", "sebelas");<br /> hasil=hasil.replaceAll("satu puluh", "sepuluh");<br /> return hasil.trim();<br /> }<br />}<br /></pre><br /></span>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-9129544825020018355.post-46472394244409125732008-12-29T21:21:00.000-08:002023-12-19T19:49:08.817-08:00How To Create Tic Tac Toe AI (Java)<h2>What is TicTacToe ?</h2>
<br />
Tic-tac-toe, also spelled tick tack toe, and alternatively called noughts and crosses, hugs and kisses, and many other names, is a pencil-and-paper game for two players, O and X, who take turns marking the spaces in a 3×3 grid, usually X going first. (source: <a href="http://en.wikipedia.org/wiki/Tic-Tac-Toe">http://en.wikipedia.org/wiki/Tic-Tac-Toe</a>)<br />
<br />
Now I will present a simple code to create Tic Tac Toe AI.<br />
<span class="fullpost"><br /><br />I will create one java file: TicTacToeAI.java<br /><br /><span style="font-weight: bold;">The Variable</span></span><br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0px;">1
2
3
4
5
6
7
8</pre>
</td><td><pre style="line-height: 125%; margin: 0px;"> <span style="color: #888888;">/* the board */</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">int</span> board<span style="color: #333333;">[][];</span>
<span style="color: #888888;">/* empty */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> EMPTY <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/* player one */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> ONE <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/* player two */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> TWO <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">;</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
<span style="font-weight: bold;">Simple Constructor</span><br />
it will initialize all value in board to 0.<br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0px;">1
2
3</pre>
</td><td><pre style="line-height: 125%; margin: 0px;"> <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">TicTacToeAI</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
board <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">][</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">];</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span class="fullpost">
<br /> <br /><span style="font-weight: bold;">Method</span><br /><br />if position (i,j) is outside the boundary it will return EMPTY</span><br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0px;">1
2
3
4
5
6</pre>
</td><td><pre style="line-height: 125%; margin: 0px;"> <span style="color: #888888;">/* get the board value for position (i,j) */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">getBoardValue</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">,</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">||</span> i <span style="color: #333333;">>=</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> EMPTY<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>j <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">||</span> j <span style="color: #333333;">>=</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> EMPTY<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">return</span> board<span style="color: #333333;">[</span>i<span style="color: #333333;">][</span>j<span style="color: #333333;">];</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span class="fullpost">
<br /> <br />My winning move definition is one single move that lead to victory.</span><br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0px;"> 1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td><td><pre style="line-height: 125%; margin: 0px;"> <span style="color: #888888;">/* calculate the winning move for current token */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #333333;">[]</span>nextWinningMove<span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> token<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>j<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span> j<span style="color: #333333;">)==</span>EMPTY<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
board<span style="color: #333333;">[</span>i<span style="color: #333333;">][</span>j<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> token<span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">boolean</span> win <span style="color: #333333;">=</span> isWin<span style="color: #333333;">(</span>token<span style="color: #333333;">);</span>
board<span style="color: #333333;">[</span>i<span style="color: #333333;">][</span>j<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> EMPTY<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>win<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">};</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
I calculate the best move based on this priority:<br />
1. lead to win<br />
2. prevent enemy to win<br />
3. center position<br />
4. first available position <br />
<!--HTML generated using hilite.me--><br />
<div style="background: rgb(255, 255, 255); border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0px;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52</pre>
</td><td><pre style="line-height: 125%; margin: 0px;"> <span style="color: #888888;">/* calculate the best move for current token */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #333333;">[]</span>nextMove<span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> token<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">/* if we can move on the next turn */</span>
<span style="color: #333399; font-weight: bold;">int</span> winMove<span style="color: #333333;">[]</span> <span style="color: #333333;">=</span> nextWinningMove<span style="color: #333333;">(</span>token<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>winMove<span style="color: #333333;">!=</span><span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> winMove<span style="color: #333333;">;</span>
<span style="color: #888888;">/* choose the move that prevent enemy to win */</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>j<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span> j<span style="color: #333333;">)==</span>EMPTY<span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
board<span style="color: #333333;">[</span>i<span style="color: #333333;">][</span>j<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> token<span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">boolean</span> ok <span style="color: #333333;">=</span> nextWinningMove<span style="color: #333333;">(</span>token<span style="color: #333333;">==</span>ONE <span style="color: #333333;">?</span> TWO <span style="color: #333333;">:</span> ONE <span style="color: #333333;">)</span> <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
board<span style="color: #333333;">[</span>i<span style="color: #333333;">][</span>j<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> EMPTY<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>ok<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">};</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">/* lucky position in the center of board*/</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)==</span>EMPTY<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">};</span>
<span style="color: #888888;">/* choose available move */</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>j<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span> j<span style="color: #333333;">)==</span>EMPTY<span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]{</span>i<span style="color: #333333;">,</span>j<span style="color: #333333;">};</span>
<span style="color: #888888;">/* no move is available */</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">/* determine if current token is win or not win */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isWin</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> token<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> DI<span style="color: #333333;">[]={-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">};</span>
<span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> DJ<span style="color: #333333;">[]={</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">};</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> j<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>j<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">;</span>j<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">/* we skip if the token in position(i,j) not equal current token */</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">,</span> j<span style="color: #333333;">)!=</span>token<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">continue</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> k<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>k<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">;</span>k<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> ctr <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>getBoardValue<span style="color: #333333;">(</span>i<span style="color: #333333;">+</span>DI<span style="color: #333333;">[</span>k<span style="color: #333333;">]*</span>ctr<span style="color: #333333;">,</span> j<span style="color: #333333;">+</span>DJ<span style="color: #333333;">[</span>k<span style="color: #333333;">]*</span>ctr<span style="color: #333333;">)==</span>token<span style="color: #333333;">)</span> ctr<span style="color: #333333;">++;</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>ctr<span style="color: #333333;">==</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span class="fullpost">
</span>Unknownnoreply@blogger.com9tag:blogger.com,1999:blog-9129544825020018355.post-91710492061228671692008-12-29T08:03:00.000-08:002008-12-29T18:35:07.849-08:00Count number of word ( C )Word in my definition is sequence of non-space character.<br />Count number of word in sentence was an easy task if you already know the trick.<br /><br />Example:<br /><br />My name is Timo ==> counted as four word<br /> I like to solve problems ==> counted as five word<br /><span class="fullpost"><br /><br />So to create a computer program that count number of word, we can count based on how many space that separate the word (trailing space will be counted once).<br /><br /><br /><pre><br />int count(char sentence[]) {<br /> int i;<br /> int ret = 0;<br /> int x = 0;<br /> for(i=0;sentence[i];i++)<br /> {<br /> if(sentence[i]!=' ')<br /> {<br /> x = 1;<br /> }<br /> else<br /> {<br /> ret += x;<br /> x = 0;<br /> }<br /> }<br /> return ret + x;<br />}<br /></pre><br /><br />Hope this tutorial will be useful :D.<br /><br /></span>Unknownnoreply@blogger.com0