Improve the othello AI
This commit is contained in:
		
							parent
							
								
									06a4cb93df
								
							
						
					
					
						commit
						1316f17dca
					
				|  | @ -229,10 +229,31 @@ export function ai(color: string, othello: Othello) { | |||
| 	const opponentColor = color == 'black' ? 'white' : 'black'; | ||||
| 
 | ||||
| 	function think() { | ||||
| 		// 打てる場所を取得
 | ||||
| 		const ps = othello.getPattern(color); | ||||
| 		if (ps.length > 0) { | ||||
| 			const p = ps[Math.floor(Math.random() * ps.length)]; | ||||
| 			othello.set(color, p[0], p[1]); | ||||
| 
 | ||||
| 		if (ps.length > 0) { // 打てる場所がある場合
 | ||||
| 			// 角を取得
 | ||||
| 			const corners = ps.filter(p => | ||||
| 				// 左上
 | ||||
| 				(p[0] == 0 && p[1] == 0) || | ||||
| 				// 右上
 | ||||
| 				(p[0] == 7 && p[1] == 0) || | ||||
| 				// 右下
 | ||||
| 				(p[0] == 7 && p[1] == 7) || | ||||
| 				// 左下
 | ||||
| 				(p[0] == 0 && p[1] == 7) | ||||
| 			); | ||||
| 
 | ||||
| 			if (corners.length > 0) { // どこかしらの角に打てる場合
 | ||||
| 				// 打てる角からランダムに選択して打つ
 | ||||
| 				const p = corners[Math.floor(Math.random() * corners.length)]; | ||||
| 				othello.set(color, p[0], p[1]); | ||||
| 			} else { // 打てる角がない場合
 | ||||
| 				// 打てる場所からランダムに選択して打つ
 | ||||
| 				const p = ps[Math.floor(Math.random() * ps.length)]; | ||||
| 				othello.set(color, p[0], p[1]); | ||||
| 			} | ||||
| 
 | ||||
| 			// 相手の打つ場所がない場合続けてAIのターン
 | ||||
| 			if (othello.getPattern(opponentColor).length === 0) { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue