今天做了一题编程题,题干很复杂:
公元208年,曹操发兵攻打荆州,刘备派诸葛亮到东吴,说服了孙权。孙刘两家联合起来共同抵抗曹军。这年冬天,孙刘大军在赤壁与曹军隔江对峙。
曹军将领常年在北方生活,不习惯坐船,许多人因为晕船根本没法打仗。于是曹操就下令将战船用铁链连在一起,上面铺上木板,这样在上面行走的时候,就好像走在平地上-样,不会晕船了。吴军将领黄盖观察到这个情况
后,就对大都督同瑜献计道,曹军的船只连在-起。我们可以采取火攻。周瑜听后点了点头,也觉得此计可行。于是黄盖先给曹操写了-封信,假称要投降曹操。
曹军现在有九艘船,三行三列排布,到了约定的这天,黄盖带着几艘轻便的小艇船载满浇了油的柴草,外面用帷幕挡上,插上旌旗前往曹营,当时刮的是东南风,顺风而行。曹换操以为这是来投降的,所以毫无防备,等到离曹营
的是东南风,顺风而行。曹操以为这是来投降的,所以毫无防备,等到离曹营只有二里左右的时候,黄盖命令士兵点燃柴草。黄盖下令士兵,将同行同列或同一斜行的三艘船点燃借助风势就可以将所有的曹军船只点燃,然而曹操也下令士兵开始阻碍火船的前进,同理,曹操如果阻碍了同行同列或同一斜行的三艘船着火,黄盖将被生擒,如果双方都没有完成,则互相撤军,已知黄盖每次只能下令点燃一艘曹军船只,曹操每次只能指挥阻碍一艘火船的前进,现在我们用九个数字代表曹军船只的位置
123
456
789
前方的斥候给孙刘大军带回来一封密函交给诸葛先生,这封密函是一-串数字代表黄将军和曹操对弃的顺序,现在让诸葛先生你来判断这场战斗究竟是谁赢了
然后是实例:
题干很复杂,一时甚至没看懂,然后同事提示说是三子棋,真乌鱼,百度了思路,用js写了,只做了简单测试
function threeChess(str) { var chessBoard = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] // let str1 = str.toString() // var chessBoard = [0,0,0,0,0,0,0,0,0] var arr = [...str.toString()] console.log(arr) arr.forEach((item, index) => { let row = parseInt(index / 3) let column = index % 3 === 0 ? 0 : index % 3 console.log(row, column) if (index % 2 !== 0) { // 曹操 chessBoard[row][column] = '#' } else { // 黄盖 chessBoard[row][column] = '*' } //判断是否分出胜负 isOver(chessBoard) //判断是否平局 if (arr.length === index || index === 9) { print('pingju') return } }) } function whoWin(chessBoard, x, y) { if (chessBoard[x][y] === '*') { console.log('huanggai WIN') return } else { console.log('caocao WIN') return } } function isOver(chessBoard) { console.log(chessBoard) //判断三行 for (var i = 0; i < 3; i++) { if ( chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][1] == chessBoard[i][2] && chessBoard[i][1] !== 0 ) { whoWin(chessBoard, i, 0) } } //判断三列 for (i = 0; i < 3; i++) { if ( chessBoard[0][i] == chessBoard[1][i] && chessBoard[1][i] == chessBoard[2][i] && chessBoard[1][i] != ' ' ) { whoWin(chessBoard, 0, i) } } //判断对角线 if ( chessBoard[0][0] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] != ' ' ) { whoWin(chessBoard, 0, 0) } if ( chessBoard[0][2] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][0] && chessBoard[1][1] != ' ' ) { whoWin(chessBoard, 0, 2) } } threeChess(5237649)
function threeChess(str) { var chessBoard = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] // let str1 = str.toString() // var chessBoard = [0,0,0,0,0,0,0,0,0] var arr = [...str.toString()] console.log(arr) arr.forEach((item, index) => { let row = parseInt(index / 3) let column = index % 3 === 0 ? 0 : index % 3 console.log(row, column) if (index % 2 !== 0) { // 曹操 chessBoard[row][column] = '#' } else { // 黄盖 chessBoard[row][column] = '*' }
//判断是否分出胜负 isOver(chessBoard) //判断是否平局 if (arr.length === index || index === 9) { print('pingju') return } }) } function whoWin(chessBoard, x, y) { if (chessBoard[x][y] === '*') { console.log('huanggai WIN')
return } else { console.log('caocao WIN') return } } function isOver(chessBoard) { console.log(chessBoard) //判断三行 for (var i = 0; i < 3; i++) { if ( chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][1] == chessBoard[i][2] && chessBoard[i][1] !== 0 ) { whoWin(chessBoard, i, 0) } } //判断三列 for (i = 0; i < 3; i++) { if ( chessBoard[0][i] == chessBoard[1][i] && chessBoard[1][i] == chessBoard[2][i] && chessBoard[1][i] != ' ' ) { whoWin(chessBoard, 0, i) } } //判断对角线 if ( chessBoard[0][0] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] != ' ' ) { whoWin(chessBoard, 0, 0) } if ( chessBoard[0][2] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][0] && chessBoard[1][1] != ' ' ) { whoWin(chessBoard, 0, 2) } }
threeChess(5237649) 标签:index,arr,编程,console,log,chessBoard,javascript,js,&& From: https://www.cnblogs.com/superarchi/p/16809140.html