questions:
https://www.codewars.com/kata/566fc12495810954b1000030/train/java
https://www.codewars.com/kata/58f5c63f1e26ecda7e000029/train/java
https://www.codewars.com/kata/51b6249c4612257ac0000005/train/typescript
https://www.codewars.com/kata/586c0909c1923fdb89002031/train/typescript
# Connect 4
```
export class Connect4 {
private game: Number[][]; // 7 columns and 6 rows
private player : Number; // 1: player 1 , 2: player 2
private hasWinner : boolean;
constructor() {
this.game = [];
this.player = 1
this.hasWinner = false
for(var i: number = 0; i < 6; i++) {
this.game[i] = [];
for(var j: number = 0; j< 7; j++) {
this.game[i][j] = 0;
}
}
}
play(col: number): string{
// 1. check if game is already finishd
if(this.hasWinner){
return "Game has finished!"
}
// 2. check if the given col is full
if(this.game[0][col] != 0){
return "Column full!"
}
// 3 placing
let level = 5
while(this.game[level][col] != 0){
level-=1
}
if(this.game[level][col] === 0){
this.game[level][col] = this.player
}else{
return "internal error"
}
// debug
this.print()
// 4. check if this move will make a winner
if(this.checkWinner(col)){
this.hasWinner = true
if(this.player === 1){
return "Player 1 wins!"
}else{
return "Player 2 wins!"
}
}
// 5. switch player and tell whos turn
let whosTurn = ""
if(this.player === 1){
whosTurn = "Player 1 has a turn"
this.player = 2
}else{
whosTurn = "Player 2 has a turn"
this.player=1
}
return whosTurn
}
print(){
console.log("\n")
for(let i=0;i<this.game.length;i++){
let str = ""
for(let j=0;j<this.game[0].length;j++){
str += String(this.game[i][j])
}
console.log(str,"\n")
}
}
// 00 01 02 03 04 05 06
// 10 11 12 13 14 15 16
// 20 21 22 23 24 25 26
// 30 31 32 33 34 35 36
// 40 41 42 43 44 45 46
// 50 51 52 53 54 55 56
checkWinner(col:number):boolean{
// 1. check vertically
for(let i=0;i<this.game.length;i++){
let row = (this.game[i])
if(this.exceeded4continues(row)){
return true
}
}
// 2. check herozontally
let subarray
for(let i = 0; i < this.game[0].length; i++){
subarray = []
for(let j = 0; j < this.game.length; j++){
subarray.push(this.game[j][i]);
}
if(this.exceeded4continues((subarray))){
return true
}
}
// 3. check diagonally
for( let k = 0 ; k <= 7 + 6 - 2; k++ ) {
subarray =[]
for( let j = 0 ; j <= k ; j++ ) {
let i = k - j;
if( i < 6 && j < 7 ) {
subarray.push(this.game[i][j]);
}
}
if(this.exceeded4continues(subarray)){
return true
}
}
return false
}
exceeded4continues(subarray:Array<any>):boolean{
let player_1 =0
let max = 0
for(let i=0;i<subarray.length;i++){
if(subarray[i] == 1){
player_1 +=1
max = Math.max(max,player_1)
}else{
player_1=0
}
}
if(max>=4){
return true
}
let player_2 =0
max =0
for(let i=0;i<subarray.length;i++){
if(subarray[i] == 2){
player_2 +=1
max = Math.max(max,player_2)
}else{
player_2=0
}
}
if(max>=4){
return true
}
return false
}
}
```
# Roman Numerals Decoder
```
export function solution(roman: string): number {
let dict = new Map();
dict.set("I",1)
dict.set("V",5)
dict.set("X",10)
dict.set("L",50)
dict.set("C",100)
dict.set("D",500)
dict.set("M",1000)
dict.set(undefined,0)
let charArray = Array.from(roman);
let total = dict.get(charArray[charArray.length-1]);
for(let i=0;i<charArray.length;i++){
let key_1 = charArray[i]
let key_2 = charArray[i-1]
if(dict.get(key_2)>=dict.get(key_1)){
total+= dict.get(key_2)
}else{
total-= dict.get(key_2)
}
}
return total
}
```
# Mexican Wave
```
import java.util.ArrayList;
public class MexicanWave {
public static String[] wave(String str) {
ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<str.length();i++){
char char_1 = str.charAt(i);
if(char_1 != ' '){
char char_2 = Character.toUpperCase(char_1);
list.add(str.substring(0,i)+char_2+str.substring(i+1));
}
}
// for(String s:list){
// System.out.println(s);
// }
return list.toArray(new String[0]);
}
}
```
# Count the Digit
```
public class CountDig {
public static int nbDig(int n, int d) {
int count = 0;
int target = String.valueOf(d).charAt(0);
for(int i=0;i<=n;i++){
String str = String.valueOf(i*i);
char [] charArray = str.toCharArray();
for(int j=0;j<charArray.length;j++){
if(charArray[j] == target){
count++;
}
}
}
return count;
}
}
```